{"id":263,"date":"2020-12-08T17:34:35","date_gmt":"2020-12-08T17:34:35","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=263"},"modified":"2021-06-15T18:07:47","modified_gmt":"2021-06-15T18:07:47","slug":"xintx-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/12\/08\/xintx-cs\/","title":{"rendered":"xIntX.cs"},"content":{"rendered":"\n<p><strong>Adjustable Bit Width Big Unsigned or Signed Integer 32,64,128,256,512,1024,2048\u2026<\/strong><\/p>\n\n\n\n<p>Updated: Jun-11,2021<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Collections;\nusing System.Collections.Generic;\nusing System.ComponentModel;\nusing System.Diagnostics;\nusing System.Globalization;\nusing System.Numerics;\nusing System.Runtime.InteropServices;\nusing System.Text;\n[Serializable]\n[StructLayout(LayoutKind.Sequential, Pack = 1)]\n[TypeConverter(typeof(xIntXConverter))]\n[DebuggerDisplay(\"{DDisplay}\")]\npublic struct xIntX : IComparable&lt;xIntX>, IComparable, IEquatable&lt;xIntX>, IConvertible, IFormattable\n{\n    private const  int    DefaultDataBitWidth = 1024;\n    private const  int    DataSize            = sizeof(uint);\n    private const  uint   AllBits             = ~(uint)0;\n    private const  int    DataSizeBits        = sizeof(uint) * 8;\n    private const  uint   HiNeg               = (uint)1 &lt;&lt; (DataSizeBits - 1);\n    private static int    _dataBitWidth;\n    private static int    DataLength;\n    private static bool   _unsigned;\n    public         uint[] Data;\n    public xIntX(xIntX value, int bitLength = 0, bool unsigned = false)\n    {\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        _unsigned  = unsigned;\n        DataLength = DataBitWidth >> 5;\n        Data       = new uint[DataLength];\n        value.Data.CopyTo(Data, 0);\n    }\n    public xIntX(string value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        if (!TryParse(value, out var result))\n            throw new Exception(\"TryParse Failed.\");\n        Data = new uint[DataLength];\n        result.Data.CopyTo(Data, 0);\n    }\n    public xIntX(byte value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        Data       = new uint[DataLength];\n        Data[0]    = value;\n    }\n    public xIntX(bool value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        Data       = new uint[DataLength];\n        Data[0]    = (uint)(value ? 1 : 0);\n    }\n    public xIntX(char value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        Data       = new uint[DataLength];\n        Data[0]    = value;\n    }\n    public xIntX(BigDecimal value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        var ba = value.WholePart.ToByteArray();\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        var len = ba.Length \/ DataSize;\n        var lim = Math.Min(len, DataLength);\n        Data = new uint[lim];\n        for (var i = 0; i &lt; lim; i++)\n            Data[i] = BitConverter.ToUInt32(ba, i * DataSize);\n    }\n    public xIntX(BigRational value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        var v1 = value.Numerator.ToByteArray();\n        DataBitWidth = bitLength;\n        DataLength   = DataBitWidth >> 5;\n        var byteCount      = v1.Length;\n        var isNegative     = _unsigned == false &amp;&amp; byteCount > 0 &amp;&amp; (v1[byteCount - 1] &amp; 0x80) == 0x80;\n        var unalignedBytes = byteCount % DataSize;\n        var dwordCount     = byteCount \/ DataSize + (unalignedBytes == 0 ? 0 : 1);\n        Data = new uint[Math.Max(dwordCount, DataLength)];\n        if (byteCount == 0)\n            return;\n        int curDword, curByte, byteInDword;\n        curByte = 3;\n        for (curDword = 0; curDword &lt; dwordCount - (unalignedBytes == 0 ? 0 : 1); curDword++)\n        {\n            byteInDword = 0;\n            while (byteInDword &lt; DataSize)\n            {\n                Data[curDword] &lt;&lt;= 8;\n                Data[curDword] |=  v1[curByte];\n                curByte--;\n                byteInDword++;\n            }\n            curByte += 8;\n        }\n        if (unalignedBytes != 0)\n        {\n            if (isNegative)\n                Data[dwordCount - 1] = 0xffffffff;\n            for (curByte = byteCount - 1; curByte >= byteCount - unalignedBytes; curByte--)\n            {\n                Data[curDword] &lt;&lt;= 8;\n                Data[curDword] |=  v1[curByte];\n            }\n        }\n        ConstructFromArray(value.Numerator.ToByteArray(), bitLength);\n    }\n    public xIntX(decimal value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        Data       = new uint[DataLength];\n        if (value &lt; 0 &amp;&amp; !_unsigned)\n        {\n            var n = -new xIntX(-value, DataBitWidth, _unsigned);\n            n.Data.CopyTo(Data, 0);\n            return;\n        }\n        var bits = decimal.GetBits(value);\n        Data[2] = (uint)bits[2];\n        Data[1] = (uint)bits[1];\n        Data[0] = (uint)bits[0];\n    }\n    public xIntX(double value, int bitLength = 0, bool unsigned = false) : this((decimal)value, bitLength, unsigned)\n    {\n    }\n    public xIntX(float value, int bitLength = 0, bool unsigned = false) : this((decimal)value, bitLength, unsigned)\n    {\n    }\n    public xIntX(short value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        Data       = new uint[DataLength];\n        if (value &lt; 0 &amp;&amp; !_unsigned)\n        {\n            var n = -new xIntX(-(value + 1), DataBitWidth, _unsigned) - 1;\n            n.Data.CopyTo(Data, 0);\n            return;\n        }\n        Data[0] = (uint)value;\n    }\n    public xIntX(int value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        Data       = new uint[DataLength];\n        if (value &lt; 0 &amp;&amp; !_unsigned)\n        {\n            var n = -new xIntX(-(value + 1), DataBitWidth, _unsigned) - 1;\n            n.Data.CopyTo(Data, 0);\n            return;\n        }\n        Data[0] = (uint)value;\n    }\n    public xIntX(long value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        Data       = new uint[DataLength];\n        if (value &lt; 0 &amp;&amp; !_unsigned)\n        {\n            var n = -new xIntX(-(value + 1), DataBitWidth, _unsigned) - 1;\n            n.Data.CopyTo(Data, 0);\n            return;\n        }\n        Data[1] = (uint)((value >> 32) &amp; 0xffffffff);\n        Data[0] = (uint)(value         &amp; 0xffffffff);\n    }\n    public xIntX(sbyte value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        Data       = new uint[DataLength];\n        if (value &lt; 0 &amp;&amp; !_unsigned)\n        {\n            var n = -new xIntX(-(value + 1), DataBitWidth, _unsigned) - 1;\n            n.Data.CopyTo(Data, 0);\n            return;\n        }\n        Data[0] = (uint)value;\n    }\n    public xIntX(ushort value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        Data       = new uint[DataLength];\n        Data[0]    = value;\n    }\n    public xIntX(uint value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        Data       = new uint[DataLength];\n        Data[0]    = value;\n    }\n    public xIntX(ulong value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        Data       = new uint[DataLength];\n        Data[1]    = (uint)((value >> 32) &amp; 0xffffffff);\n        Data[0]    = (uint)(value         &amp; 0xffffffff);\n    }\n    public xIntX(BigInteger value, int bitLength = 0, bool unsigned = false) : this(value.ToByteArray(), bitLength, unsigned)\n    {\n    }\n    public xIntX(Guid value, int bitLength = 0, bool unsigned = false) : this(value.ToByteArray(), bitLength, unsigned)\n    {\n    }\n    public xIntX(byte[] value, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        var minSize = value.Length \/ DataSize;\n        if (value == null)\n            throw new ArgumentNullException(\"value\");\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        var byteCount      = value.Length;\n        var isNegative     = _unsigned == false &amp;&amp; byteCount > 0 &amp;&amp; (value[byteCount - 1] &amp; 0x80) == 0x80;\n        var unalignedBytes = byteCount % DataSize;\n        var dwordCount     = byteCount \/ DataSize + (unalignedBytes == 0 ? 0 : 1);\n        Data = new uint[Math.Max(dwordCount, DataLength)];\n        if (byteCount == 0)\n            return;\n        int curDword, curByte, byteInDword;\n        curByte = 3;\n        for (curDword = 0; curDword &lt; dwordCount - (unalignedBytes == 0 ? 0 : 1); curDword++)\n        {\n            byteInDword = 0;\n            while (byteInDword &lt; DataSize)\n            {\n                Data[curDword] &lt;&lt;= 8;\n                Data[curDword] |=  value[curByte];\n                curByte--;\n                byteInDword++;\n            }\n            curByte += 8;\n        }\n        if (unalignedBytes != 0)\n        {\n            if (isNegative)\n                Data[dwordCount - 1] = 0xffffffff;\n            for (curByte = byteCount - 1; curByte >= byteCount - unalignedBytes; curByte--)\n            {\n                Data[curDword] &lt;&lt;= 8;\n                Data[curDword] |=  value[curByte];\n            }\n        }\n    }\n    public xIntX(int sign, uint[] array, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        if (array == null)\n            throw new Exception(\"Array cannot be null.\");\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        Data       = new uint[DataLength];\n        var ba = new byte[DataSize];\n        for (var i = 0; i &lt; Math.Min(DataLength, array.Length); i++)\n        {\n            Array.Copy(BitConverter.GetBytes(array[i]), 0, ba, 0, DataSize);\n            Data[i] = BitConverter.ToUInt32(ba, 0);\n        }\n        if (!_unsigned)\n        {\n            if (sign &lt; 0)\n                Data[DataLength - 1] |= HiNeg;\n            else\n                Data[DataLength - 1] &amp;= ~HiNeg;\n        }\n    }\n    public xIntX(uint[] array, int bitLength = 0, bool unsigned = false)\n    {\n        _unsigned = unsigned;\n        if (array == null)\n            throw new Exception(\"Array cannot be null.\");\n        if (bitLength == 0)\n            DataBitWidth  = DefaultDataBitWidth;\n        else DataBitWidth = bitLength;\n        DataLength = DataBitWidth >> 5;\n        if (array.Length != DataLength)\n            Array.Resize(ref array, DataLength);\n        Data = new uint[DataLength];\n        var ba = new byte[DataSize];\n        for (var i = 0; i &lt; Data.Length; i++)\n        {\n            Array.Copy(BitConverter.GetBytes(array[i]), 0, ba, 0, DataSize);\n            Data[i] = BitConverter.ToUInt32(ba, 0);\n        }\n    }\n    public bool Unsigned\n    {\n        get => _unsigned;\n        set => _unsigned = value;\n    }\n    private static int DataBitWidth\n    {\n        get => _dataBitWidth;\n        set\n        {\n            _dataBitWidth = value;\n            if (_dataBitWidth &lt; 32)\n                _dataBitWidth = 32;\n        }\n    }\n    [DebuggerBrowsable(DebuggerBrowsableState.Never)]\n    private string DDisplay => ToString();\n    public xIntX MaxValue\n    {\n        get\n        {\n            var r = new xIntX(0, DataBitWidth, _unsigned);\n            for (var i = 0; i &lt; r.Data.Length; ++i)\n                r.Data[i] = uint.MaxValue;\n            r.Data[r.Data.Length - 1] = int.MaxValue;\n            return r;\n        }\n    }\n    public int BitWidth\n    {\n        get\n        {\n            xIntX bw = 1;\n            var   v  = new xIntX(this, DataBitWidth, _unsigned);\n            while ((v >>= 1) > 0)\n                bw++;\n            if (bw &lt; 8)\n                bw = 8;\n            while (bw % 8 != 0)\n                bw++;\n            return (int)bw;\n        }\n    }\n    public float MaxDecimalPlaces => DataBitWidth \/ 64f * 20f;\n    public int DecimalPlaces\n    {\n        get\n        {\n            var a       = new xIntX(this, DataBitWidth, _unsigned);\n            var dPlaces = 0;\n            if (a.Sign == 0)\n                return 1;\n            if (a.Sign &lt; 0)\n                try\n                {\n                    a = -a;\n                }\n                catch (Exception ex)\n                {\n                    return 0;\n                }\n            var biRadix = new xIntX(10, DataBitWidth, _unsigned);\n            while (a > 0)\n                try\n                {\n                    Divide(a, biRadix, out var remainder, out var quotient);\n                    a = quotient;\n                    dPlaces++;\n                }\n                catch (Exception ex)\n                {\n                    break;\n                }\n            return dPlaces;\n        }\n    }\n    public int Sign\n    {\n        get\n        {\n            if (_unsigned)\n                return 1;\n            var allZero = true;\n            var ba      = Data;\n            for (var i = 0; i &lt; ba.Length; i++)\n                if (ba[i] != 0)\n                {\n                    allZero = false;\n                    break;\n                }\n            if (allZero)\n                return 0;\n            return (Data[Data.Length - 1] &amp; HiNeg) == 0 ? 1 : -1;\n        }\n    }\n    public bool IsOne      => this       == 1;\n    public bool IsEven     => (this &amp; 1) == 0;\n    public bool IsNegative => Sign       &lt; 0;\n    public bool IsZero\n    {\n        get\n        {\n            for (var i = 0; i &lt; Data.Length; i++)\n                if (Data[i] != 0)\n                    return false;\n            return true;\n        }\n    }\n    public int DataUsed\n    {\n        get\n        {\n            var DataUsed = Data.Length;\n            if (!IsNegative)\n            {\n                while (DataUsed > 1 &amp;&amp; Data[DataUsed - 1] == 0)\n                    --DataUsed;\n                if (DataUsed == 0)\n                    DataUsed = 1;\n            }\n            return DataUsed;\n        }\n    }\n    int IComparable.CompareTo(object obj)\n    {\n        return Compare(this, obj);\n    }\n    public int CompareTo(xIntX value)\n    {\n        return Compare(this, value);\n    }\n    TypeCode IConvertible.GetTypeCode()\n    {\n        return TypeCode.Object;\n    }\n    bool IConvertible.ToBoolean(IFormatProvider provider)\n    {\n        return (bool)this;\n    }\n    byte IConvertible.ToByte(IFormatProvider provider)\n    {\n        return (byte)this;\n    }\n    char IConvertible.ToChar(IFormatProvider provider)\n    {\n        return (char)this;\n    }\n    DateTime IConvertible.ToDateTime(IFormatProvider provider)\n    {\n        throw new InvalidCastException();\n    }\n    decimal IConvertible.ToDecimal(IFormatProvider provider)\n    {\n        return (decimal)this;\n    }\n    double IConvertible.ToDouble(IFormatProvider provider)\n    {\n        return (double)this;\n    }\n    short IConvertible.ToInt16(IFormatProvider provider)\n    {\n        return (short)this;\n    }\n    int IConvertible.ToInt32(IFormatProvider provider)\n    {\n        return (int)this;\n    }\n    long IConvertible.ToInt64(IFormatProvider provider)\n    {\n        return (long)this;\n    }\n    sbyte IConvertible.ToSByte(IFormatProvider provider)\n    {\n        return (sbyte)this;\n    }\n    float IConvertible.ToSingle(IFormatProvider provider)\n    {\n        return (float)this;\n    }\n    string IConvertible.ToString(IFormatProvider provider)\n    {\n        return ToString(null, provider);\n    }\n    public object ToType(Type conversionType, IFormatProvider provider)\n    {\n        object value;\n        if (TryConvert(conversionType, provider, out value))\n            return value;\n        throw new InvalidCastException();\n    }\n    ushort IConvertible.ToUInt16(IFormatProvider provider)\n    {\n        if (Data[1] != 0)\n            throw new OverflowException();\n        return Convert.ToUInt16(Data[0]);\n    }\n    uint IConvertible.ToUInt32(IFormatProvider provider)\n    {\n        if (Data[1] != 0)\n            throw new OverflowException();\n        return Convert.ToUInt32(Data[0]);\n    }\n    ulong IConvertible.ToUInt64(IFormatProvider provider)\n    {\n        if (Data[1] != 0)\n            return ((ulong)Data[1] &lt;&lt; 32) | Data[0];\n        return Data[0];\n    }\n    public bool Equals(xIntX obj)\n    {\n        if (ReferenceEquals(obj, null))\n            return false;\n        if (ReferenceEquals(this, obj))\n            return true;\n        if (Data.Length != obj.Data.Length)\n            return false;\n        if (Sign != obj.Sign)\n            return false;\n        for (var i = 0; i &lt; Data.Length; i++)\n            if (Data[i] != obj.Data[i])\n                return false;\n        return true;\n    }\n    public string ToString(string format, IFormatProvider formatProvider)\n    {\n        if (formatProvider == null)\n            formatProvider = CultureInfo.CurrentCulture;\n        if (!string.IsNullOrEmpty(format))\n        {\n            var ch = format[0];\n            if (ch == 'x' || ch == 'X')\n            {\n                int.TryParse(format.Substring(1).Trim(), out var min);\n                return ToHexString(ch == 'X');\n            }\n            if (ch != 'G' &amp;&amp; ch != 'g' &amp;&amp; ch != 'D' &amp;&amp; ch != 'd')\n                throw new NotSupportedException(\"Not supported format: \" + format);\n        }\n        return ToString((NumberFormatInfo)formatProvider.GetFormat(typeof(NumberFormatInfo)), 10);\n    }\n    private static byte[] ToByteArray(ulong[] value)\n    {\n        var ba = new byte[value.Length &lt;&lt; 3];\n        Buffer.BlockCopy(value, 0, ba, 0, value.Length &lt;&lt; 3);\n        return ba;\n    }\n    private static byte[] ToByteArray(uint[] value)\n    {\n        var ba = new byte[value.Length &lt;&lt; 2];\n        Buffer.BlockCopy(value, 0, ba, 0, value.Length &lt;&lt; 2);\n        return ba;\n    }\n    public override int GetHashCode()\n    {\n        static uint CombineHash(uint u1, uint u2)\n        {\n            return ((u1 &lt;&lt; 7) | (u1 >> 25)) ^ u2;\n        }\n        var s = Sign;\n        var i = Data.Length;\n        while (--i >= 0)\n            s = (int)CombineHash((uint)s, Data[i]);\n        return s;\n    }\n    public static byte[] GetBytesInt(xIntX value)\n    {\n        var b  = value.Sign.GetBytes();\n        var tb = b.Add(value.Data.GetBytes());\n        return tb;\n    }\n    public override bool Equals(object obj)\n    {\n        return base.Equals(obj);\n    }\n    public override string ToString()\n    {\n        return ToString(null, null);\n    }\n    public string ToString(string format)\n    {\n        return ToString(format, null);\n    }\n    public string ToHexString(bool caps)\n    {\n        var bytes = ToByteArray().Invert();\n        var sb    = new StringBuilder();\n        var x     = caps ? \"X\" : \"x\";\n        foreach (var b in bytes)\n        {\n            var hex = b.ToString($\"{x}2\");\n            sb.Append(hex);\n        }\n        return sb.ToString();\n    }\n    private string ToString(NumberFormatInfo info, int radix)\n    {\n        if (radix &lt; 2 || radix > 36)\n            throw new ArgumentOutOfRangeException(\"radix\");\n        if (Sign == 0)\n            return \"0\";\n        var negative = Sign &lt; 0;\n        var a        = new xIntX(this, DataBitWidth, _unsigned);\n        if (negative)\n            try\n            {\n                a = -a;\n            }\n            catch (Exception ex)\n            {\n            }\n        var          biRadix = new xIntX(radix, DataBitWidth, _unsigned);\n        const string charSet = \"0123456789abcdefghijklmnopqrstuvwxyz\";\n        var          al      = new ArrayList();\n        while (a > 0)\n            try\n            {\n                Divide(a, biRadix, out var remainder, out var quotient);\n                al.Insert(0, charSet[(int)remainder.Data[0]]);\n                a = quotient;\n            }\n            catch (Exception ex)\n            {\n                break;\n            }\n        var result = new string((char[])al.ToArray(typeof(char)));\n        if (radix == 10 &amp;&amp; negative)\n            return \"-\" + result;\n        return result;\n    }\n    public static xIntX Abs(xIntX value)\n    {\n        if (ReferenceEquals(value, null))\n            throw new ArgumentNullException(\"value\");\n        if (value.Sign &lt; 0)\n            return -value;\n        return value;\n    }\n    public bool TryConvert(Type conversionType, IFormatProvider provider, out object value)\n    {\n        if (conversionType == typeof(bool))\n        {\n            value = (bool)this;\n            return true;\n        }\n        if (conversionType == typeof(byte))\n        {\n            value = (byte)this;\n            return true;\n        }\n        if (conversionType == typeof(char))\n        {\n            value = (char)this;\n            return true;\n        }\n        if (conversionType == typeof(decimal))\n        {\n            value = (decimal)this;\n            return true;\n        }\n        if (conversionType == typeof(double))\n        {\n            value = (double)this;\n            return true;\n        }\n        if (conversionType == typeof(short))\n        {\n            value = (short)this;\n            return true;\n        }\n        if (conversionType == typeof(int))\n        {\n            value = (int)this;\n            return true;\n        }\n        if (conversionType == typeof(long))\n        {\n            value = (long)this;\n            return true;\n        }\n        if (conversionType == typeof(sbyte))\n        {\n            value = (sbyte)this;\n            return true;\n        }\n        if (conversionType == typeof(float))\n        {\n            value = (float)this;\n            return true;\n        }\n        if (conversionType == typeof(string))\n        {\n            value = ToString(null, provider);\n            return true;\n        }\n        if (conversionType == typeof(ushort))\n        {\n            value = (ushort)this;\n            return true;\n        }\n        if (conversionType == typeof(uint))\n        {\n            value = (uint)this;\n            return true;\n        }\n        if (conversionType == typeof(ulong))\n        {\n            value = (ulong)this;\n            return true;\n        }\n        if (conversionType == typeof(byte[]))\n        {\n            value = ToByteArray();\n            return true;\n        }\n        if (conversionType == typeof(Guid))\n        {\n            value = new Guid(ToByteArray());\n            return true;\n        }\n        value = null;\n        return false;\n    }\n    public static xIntX Parse(string value)\n    {\n        return Parse(value, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);\n    }\n    public static xIntX Parse(string value, NumberStyles style)\n    {\n        return Parse(value, style, NumberFormatInfo.CurrentInfo);\n    }\n    public static xIntX Parse(string value, IFormatProvider provider)\n    {\n        return Parse(value, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));\n    }\n    public static xIntX Parse(string value, NumberStyles style, IFormatProvider provider)\n    {\n        if (!TryParse(value, style, provider, out var result))\n            throw new Exception($\"TryParse value {value} failure.\");\n        return result;\n    }\n    public static bool TryParse(string value, out xIntX result)\n    {\n        return TryParse(value, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);\n    }\n    public static bool TryParse(string value, NumberStyles style, IFormatProvider provider, out xIntX result)\n    {\n        result = 0;\n        if (string.IsNullOrEmpty(value))\n            return false;\n        if (value.StartsWith(\"x\", StringComparison.OrdinalIgnoreCase))\n        {\n            style |= NumberStyles.AllowHexSpecifier;\n            value =  value.Substring(1);\n        }\n        else\n        {\n            if (value.StartsWith(\"0x\", StringComparison.OrdinalIgnoreCase))\n            {\n                style |= NumberStyles.AllowHexSpecifier;\n                value =  value.Substring(2);\n            }\n        }\n        if ((style &amp; NumberStyles.AllowHexSpecifier) == NumberStyles.AllowHexSpecifier)\n            return TryParseNum(value, 16, out result);\n        return TryParseNum(value, 10, out result);\n    }\n    public static bool TryParseNum(string digits, int radix, out xIntX result)\n    {\n        result = new xIntX(0, DataBitWidth, _unsigned);\n        if (digits == null)\n            return false;\n        var multiplier = new xIntX(1, DataBitWidth * 2, _unsigned);\n        digits = digits.ToUpper(CultureInfo.CurrentCulture).Trim();\n        var nDigits = digits[0] == '-' ? 1 : 0;\n        for (var idx = digits.Length - 1; idx >= nDigits; idx--)\n        {\n            var d = (int)digits[idx];\n            if (d != 48)\n            {\n                var a = 1;\n            }\n            if (d >= '0' &amp;&amp; d &lt;= '9')\n                d -= '0';\n            else if (d >= 'A' &amp;&amp; d &lt;= 'Z')\n                d = d - 'A' + 10;\n            else\n                return false;\n            if (d >= radix)\n                return false;\n            result     += multiplier * d;\n            multiplier *= radix;\n            if (multiplier.DataUsed > DataLength)\n                throw new Exception($\"Data overflow in Multiplier {new StackFrame(1, true).GetFileLineNumber()} \");\n        }\n        if (digits[0] == '-' &amp;&amp; !_unsigned)\n            result = -result;\n        return true;\n    }\n    public static int Compare(xIntX left, object right)\n    {\n        if (right is xIntX)\n            return Compare(left, (xIntX)right);\n        if (right is bool)\n            return Compare(left, new xIntX((bool)right, DataBitWidth, _unsigned));\n        if (right is byte)\n            return Compare(left, new xIntX((byte)right, DataBitWidth, _unsigned));\n        if (right is char)\n            return Compare(left, new xIntX((char)right, DataBitWidth, _unsigned));\n        if (right is decimal)\n            return Compare(left, new xIntX((decimal)right, DataBitWidth, _unsigned));\n        if (right is double)\n            return Compare(left, new xIntX((double)right, DataBitWidth, _unsigned));\n        if (right is short)\n            return Compare(left, new xIntX((short)right, DataBitWidth, _unsigned));\n        if (right is int)\n            return Compare(left, new xIntX((int)right, DataBitWidth, _unsigned));\n        if (right is long)\n            return Compare(left, new xIntX((long)right, DataBitWidth, _unsigned));\n        if (right is sbyte)\n            return Compare(left, new xIntX((sbyte)right, DataBitWidth, _unsigned));\n        if (right is float)\n            return Compare(left, new xIntX((float)right, DataBitWidth, _unsigned));\n        if (right is ushort)\n            return Compare(left, new xIntX((ushort)right, DataBitWidth, _unsigned));\n        if (right is uint)\n            return Compare(left, new xIntX((uint)right, DataBitWidth, _unsigned));\n        if (right is ulong)\n            return Compare(left, new xIntX((ulong)right, DataBitWidth, _unsigned));\n        var bytes = right as byte[];\n        if (bytes != null)\n            return Compare(left, new xIntX(bytes, DataBitWidth, _unsigned));\n        if (right is Guid)\n            return Compare(left, new xIntX((Guid)right, DataBitWidth, _unsigned));\n        throw new ArgumentException();\n    }\n    public static int Compare(xIntX left, xIntX right)\n    {\n        if (ReferenceEquals(left, right))\n            return 0;\n        if (left.Sign >= 0 &amp;&amp; right.Sign &lt; 0)\n            return 1;\n        if (left.Sign &lt; 0 &amp;&amp; right.Sign >= 0)\n            return -1;\n        if (left.Data.Length != right.Data.Length)\n            return -1;\n        for (var i = left.Data.Length - 1; i > 0; i--)\n            if (left.Data[i] != right.Data[i])\n                return left.Data[i].CompareTo(right.Data[i]);\n        return left.Data[0].CompareTo(right.Data[0]);\n    }\n    public static implicit operator xIntX(bool value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static implicit operator xIntX(byte value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static implicit operator xIntX(char value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static explicit operator xIntX(decimal value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static explicit operator xIntX(double value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static implicit operator xIntX(short value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static implicit operator xIntX(int value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static implicit operator xIntX(long value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static implicit operator xIntX(sbyte value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static explicit operator xIntX(float value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static implicit operator xIntX(ushort value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static implicit operator xIntX(uint value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static implicit operator xIntX(ulong value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static implicit operator xIntX(BigInteger value)\n    {\n        if (DataBitWidth == 0)\n            DataBitWidth = value.GetBitWidth();\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static implicit operator xIntX(BigRational value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static implicit operator xIntX(BigDecimal value)\n    {\n        return new xIntX(value, DataBitWidth, _unsigned);\n    }\n    public static explicit operator bool(xIntX value)\n    {\n        return (byte)value.Data[0] != 0;\n    }\n    public static explicit operator byte(xIntX value)\n    {\n        return (byte)value.Data[0];\n    }\n    public static explicit operator char(xIntX value)\n    {\n        return (char)(ushort)value.Data[0];\n    }\n    public static explicit operator decimal(xIntX value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        if (value.Data.Length == 1)\n            return new decimal((int)value.Data[0], 0, 0, value.Sign &lt; 0, 0);\n        if (value.Data.Length == 2)\n            return new decimal((int)value.Data[0], (int)value.Data[1], 0, value.Sign &lt; 0, 0);\n        if (value.Data.Length == 3)\n            return new decimal((int)value.Data[0], (int)value.Data[1], (int)value.Data[2], value.Sign &lt; 0, 0);\n        throw new ArgumentException(\"Value length exceeds decimal length.\");\n    }\n    public static explicit operator double(xIntX value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        var nfi = CultureInfo.InvariantCulture.NumberFormat;\n        if (!double.TryParse(value.ToString(nfi, 10), NumberStyles.Number, nfi, out var d))\n            throw new OverflowException();\n        return d;\n    }\n    public static explicit operator float(xIntX value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        var nfi = CultureInfo.InvariantCulture.NumberFormat;\n        if (!float.TryParse(value.ToString(nfi, 10), NumberStyles.Number, nfi, out var f))\n            throw new OverflowException();\n        return f;\n    }\n    public static explicit operator short(xIntX value)\n    {\n        if (value.Data[0] > 0x8000)\n            throw new OverflowException();\n        if (value.Data[0] == 0x8000 &amp;&amp; value.Sign > 0)\n            throw new OverflowException();\n        return (short)((int)value.Data[0] * value.Sign);\n    }\n    public static explicit operator int(xIntX value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        return (int)value.Data[0] * value.Sign;\n    }\n    public static explicit operator long(xIntX value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        if (value.Data[0] > int.MaxValue)\n            throw new OverflowException();\n        if (value.Data.Length > 1)\n            if (value.Data[1] != 0)\n                return (long)(((ulong)value.Data[1] &lt;&lt; 32) | value.Data[0]) * value.Sign;\n        return value.Data[0] * value.Sign;\n    }\n    public static explicit operator uint(xIntX value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        return value.Data[0];\n    }\n    public static explicit operator ushort(xIntX value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        return (ushort)value.Data[0];\n    }\n    public static explicit operator ulong(xIntX value)\n    {\n        if (value.Data.Length > 1)\n            if (value.Data[1] != 0)\n                return ((ulong)value.Data[1] &lt;&lt; 32) | value.Data[0];\n        return value.Data[0];\n    }\n    public static explicit operator BigInteger(xIntX value)\n    {\n        return new BigInteger(value.ToByteArray());\n    }\n    public static explicit operator BigDecimal(xIntX value)\n    {\n        return new xIntX(value.ToByteArray());\n    }\n    public static explicit operator BigRational(xIntX value)\n    {\n        return new BigRational(new BigInteger(value.ToByteArray()));\n    }\n    public static bool operator >(xIntX left, xIntX right)\n    {\n        return left.CompareTo(right) > 0;\n    }\n    public static bool operator &lt;(xIntX left, xIntX right)\n    {\n        return Compare(left, right) &lt; 0;\n    }\n    public static bool operator >=(xIntX left, xIntX right)\n    {\n        return Compare(left, right) >= 0;\n    }\n    public static bool operator &lt;=(xIntX left, xIntX right)\n    {\n        return Compare(left, right) &lt;= 0;\n    }\n    public static bool operator !=(xIntX left, xIntX right)\n    {\n        return !left.Equals(right);\n    }\n    public static bool operator ==(xIntX left, xIntX right)\n    {\n        return left.Equals(right);\n    }\n    public static xIntX operator +(xIntX value)\n    {\n        return value;\n    }\n    public static xIntX operator ~(xIntX value)\n    {\n        var da = new uint[DataLength];\n        for (var idx = 0; idx &lt; DataLength; idx++)\n            da[idx] = ~value.Data[idx];\n        return new xIntX(da, DataBitWidth);\n    }\n    public static xIntX operator -(xIntX value)\n    {\n        if (ReferenceEquals(value, null))\n            throw new ArgumentNullException(\"value\");\n        if (value.IsZero)\n            return 0;\n        var da = new uint[DataLength];\n        for (var i = 0; i &lt; da.Length; i++)\n            da[i] = ~value.Data[i];\n        var carry = true;\n        var index = 0;\n        while (carry &amp;&amp; index &lt; da.Length)\n        {\n            var val = (long)da[index] + 1;\n            da[index] = (uint)(val &amp; AllBits);\n            carry     = val >> DataSizeBits > 0;\n            index++;\n        }\n        return new xIntX(da, DataBitWidth);\n    }\n    public static xIntX operator ++(xIntX value)\n    {\n        return value + 1;\n    }\n    public static xIntX operator --(xIntX value)\n    {\n        return value - 1;\n    }\n    public static xIntX Negate(xIntX value)\n    {\n        var ldata = (uint[])value.Data.Clone();\n        for (var i = 0; i &lt; value.Data.Length; i++)\n            ldata[i] = ~value.Data[i];\n        return new xIntX(value.Sign, ldata, DataBitWidth, _unsigned);\n    }\n    public static xIntX operator +(xIntX left, xIntX right)\n    {\n        if (right.IsZero)\n            return left;\n        if (left.IsZero)\n            return right;\n        var  dl     = Math.Max(left.Data.Length, right.Data.Length);\n        var  lim    = Math.Min(left.Data.Length, right.Data.Length);\n        var  result = new uint[dl];\n        long carry  = 0;\n        for (var i = 0; i &lt; dl &amp;&amp; i &lt; lim; i++)\n        {\n            var sum = left.Data[i] + (long)right.Data[i] + carry;\n            carry     = sum >> 32;\n            result[i] = (uint)(sum &amp; 0xFFFFFFFF);\n        }\n        if (carry != 0)\n        {\n            var idx = 0;\n            while (idx &lt; result.Length - 1)\n            {\n                if (result[idx] == 0)\n                    break;\n                idx++;\n            }\n            result[idx] = (uint)carry;\n        }\n        return new xIntX(left.Sign * right.Sign, result, DataBitWidth, _unsigned);\n    }\n    public static xIntX operator -(xIntX left, xIntX right)\n    {\n        if (right.IsZero)\n            return left;\n        if (left.IsZero)\n            return -right;\n        var  size  = Math.Max(left.Data.Length, right.Data.Length) + 1;\n        var  da    = new uint[size];\n        long carry = 0;\n        for (var i = 0; i &lt; DataLength &amp;&amp; i &lt; left.Data.Length &amp;&amp; i &lt; right.Data.Length; i++)\n        {\n            var diff = left.Data[i] - (long)right.Data[i] - carry;\n            da[i] = (uint)(diff &amp; AllBits);\n            carry = diff &lt; 0 ? 1 : 0;\n        }\n        return new xIntX(da, DataBitWidth);\n    }\n    public static xIntX Add(xIntX left, xIntX right)\n    {\n        return left + right;\n    }\n    public static xIntX Subtract(xIntX left, xIntX right)\n    {\n        return left - right;\n    }\n    public static xIntX Divide(xIntX dividend, xIntX divisor)\n    {\n        if (divisor == 0)\n            throw new DivideByZeroException();\n        return DivRem(dividend, divisor, out var integer);\n    }\n    public static void Divide(xIntX dividend, xIntX divisor, out xIntX remainder, out xIntX quotient)\n    {\n        if (divisor == 0)\n            throw new DivideByZeroException();\n        DivRem(dividend.Data, divisor.Data, out var quo, out var rem);\n        remainder = new xIntX(1,                            rem, DataBitWidth, _unsigned);\n        quotient  = new xIntX(dividend.Sign * divisor.Sign, quo, DataBitWidth, _unsigned);\n    }\n    public static xIntX DivRem(xIntX dividend, xIntX divisor, out xIntX remainder)\n    {\n        if (divisor == 0)\n            throw new DivideByZeroException();\n        DivRem(dividend.Data, divisor.Data, out var quotient, out var rem);\n        remainder = new xIntX(1, rem, DataBitWidth, _unsigned);\n        return new xIntX(dividend.Sign * divisor.Sign, quotient, DataBitWidth, _unsigned);\n    }\n    private static void DivRem(uint[] dividend, uint[] divisor, out uint[] quotient, out uint[] remainder)\n    {\n        const ulong hiBit       = 0x100000000;\n        var         divisorLen  = GetLength(divisor);\n        var         dividendLen = GetLength(dividend);\n        if (divisorLen &lt;= 1)\n        {\n            ulong rem = 0;\n            var   div = divisor[0];\n            quotient  = new uint[dividendLen];\n            remainder = new uint[1];\n            for (var i = dividendLen - 1; i >= 0; i--)\n            {\n                rem *= hiBit;\n                rem += dividend[i];\n                var q = rem \/ div;\n                rem         -= q * div;\n                quotient[i] =  (uint)q;\n            }\n            remainder[0] = (uint)rem;\n            return;\n        }\n        if (dividendLen >= divisorLen)\n        {\n            var shift        = GetNormalizeShift(divisor[divisorLen - 1]);\n            var normDividend = new uint[dividendLen + 1];\n            var normDivisor  = new uint[divisorLen];\n            Normalize(dividend, dividendLen, normDividend, shift);\n            Normalize(divisor,  divisorLen,  normDivisor,  shift);\n            quotient = new uint[dividendLen - divisorLen + 1];\n            for (var j = dividendLen - divisorLen; j >= 0; j--)\n            {\n                var dx = hiBit * normDividend[j + divisorLen] + normDividend[j + divisorLen - 1];\n                var qj = dx \/ normDivisor[divisorLen                                        - 1];\n                dx -= qj * normDivisor[divisorLen - 1];\n                do\n                {\n                    if (qj &lt; hiBit &amp;&amp; qj * normDivisor[divisorLen - 2] &lt;= dx * hiBit + normDividend[j + divisorLen - 2])\n                        break;\n                    qj -= 1L;\n                    dx += normDivisor[divisorLen - 1];\n                } while (dx &lt; hiBit);\n                ulong di = 0;\n                ulong dj;\n                var   index = 0;\n                while (index &lt; divisorLen)\n                {\n                    var dqj = normDivisor[index] * qj;\n                    dj                      = normDividend[index + j] - (uint)dqj - di;\n                    normDividend[index + j] = (uint)dj;\n                    dqj                     = dqj >> 32;\n                    dj                      = dj  >> 32;\n                    di                      = dqj - dj;\n                    index++;\n                }\n                dj                           = normDividend[j + divisorLen] - di;\n                normDividend[j + divisorLen] = (uint)dj;\n                quotient[j]                  = (uint)qj;\n                if ((long)dj &lt; 0)\n                {\n                    quotient[j]--;\n                    ulong sum = 0;\n                    for (index = 0; index &lt; divisorLen; index++)\n                    {\n                        sum                     = normDivisor[index] + normDividend[j + index] + sum;\n                        normDividend[j + index] = (uint)sum;\n                        sum                     = sum >> 32;\n                    }\n                    sum += normDividend[j + divisorLen];\n                    normDividend[j        + divisorLen] = (uint)sum;\n                }\n            }\n            remainder = Unnormalize(normDividend, shift);\n            return;\n        }\n        quotient  = new uint[1];\n        remainder = dividend;\n    }\n    private static int GetLength(uint[] uints)\n    {\n        var index = uints.Length - 1;\n        while (index >= 0 &amp;&amp; uints[index] == 0)\n            index--;\n        return index + 1;\n    }\n    private static int GetNormalizeShift(uint ui)\n    {\n        var shift = 0;\n        if ((ui &amp; 0xffff0000) == 0)\n        {\n            ui    =  ui &lt;&lt; 16;\n            shift += 16;\n        }\n        if ((ui &amp; 0xff000000) == 0)\n        {\n            ui    =  ui &lt;&lt; 8;\n            shift += 8;\n        }\n        if ((ui &amp; 0xf0000000) == 0)\n        {\n            ui    =  ui &lt;&lt; 4;\n            shift += 4;\n        }\n        if ((ui &amp; 0xc0000000) == 0)\n        {\n            ui    =  ui &lt;&lt; 2;\n            shift += 2;\n        }\n        if ((ui &amp; 0x80000000) == 0)\n            shift++;\n        return shift;\n    }\n    private static uint[] Unnormalize(uint[] normalized, int shift)\n    {\n        var len          = GetLength(normalized);\n        var unnormalized = new uint[len];\n        if (shift > 0)\n        {\n            var  rshift = 32 - shift;\n            uint r      = 0;\n            for (var i = len - 1; i >= 0; i--)\n            {\n                unnormalized[i] = (normalized[i] >> shift) | r;\n                r               = normalized[i] &lt;&lt; rshift;\n            }\n        }\n        else\n        {\n            for (var j = 0; j &lt; len; j++)\n                unnormalized[j] = normalized[j];\n        }\n        return unnormalized;\n    }\n    private static void Normalize(uint[] unormalized, int len, uint[] normalized, int shift)\n    {\n        int  i;\n        uint n = 0;\n        if (shift > 0)\n        {\n            var rShift = 32 - shift;\n            for (i = 0; i &lt; len; i++)\n            {\n                normalized[i] = (unormalized[i] &lt;&lt; shift) | n;\n                n             = unormalized[i] >> rShift;\n            }\n        }\n        else\n        {\n            i = 0;\n            while (i &lt; len)\n            {\n                normalized[i] = unormalized[i];\n                i++;\n            }\n        }\n        while (i &lt; normalized.Length)\n            normalized[i++] = 0;\n        if (n != 0)\n            normalized[len] = n;\n    }\n    public static xIntX Remainder(xIntX dividend, xIntX divisor)\n    {\n        DivRem(dividend, divisor, out var remainder);\n        return remainder;\n    }\n    public static xIntX Max(xIntX left, xIntX right)\n    {\n        return left.CompareTo(right) &lt; 0 ? right : left;\n    }\n    public static xIntX Min(xIntX left, xIntX right)\n    {\n        return left.CompareTo(right) &lt;= 0 ? left : right;\n    }\n    public static xIntX operator %(xIntX dividend, xIntX divisor)\n    {\n        return Remainder(dividend, divisor);\n    }\n    public static xIntX operator \/(xIntX dividend, xIntX divisor)\n    {\n        return Divide(dividend, divisor);\n    }\n    public ulong[] ToUIn64Array()\n    {\n        var al = Data.Length >> 1;\n        if (al * 2 != Data.Length)\n            al++;\n        var arr = new ulong[al];\n        Buffer.BlockCopy(Data, 0, arr, 0, Data.Length &lt;&lt; 2);\n        return arr;\n    }\n    public uint[] ToUIn32Array()\n    {\n        return Data;\n    }\n    public byte[] ToByteArray()\n    {\n        var ba = new byte[Data.Length * DataSize];\n        Buffer.BlockCopy(Data, 0, ba, 0, Data.Length * DataSize);\n        return ba;\n    }\n    private void TrimToMsb()\n    {\n        var dataUsed = Data.Length;\n        while (dataUsed > 1 &amp;&amp; Data[dataUsed - 1] == 0)\n            --dataUsed;\n        if (dataUsed != Data.Length)\n        {\n            var tData = new uint[dataUsed];\n            for (var i = 0; i &lt; dataUsed; i++)\n                tData[i] = Data[i];\n            Data = (uint[])tData.Clone();\n        }\n    }\n    public static xIntX Multiply(xIntX left, xIntX right)\n    {\n        if (left == 0 || right == 0)\n            return 0;\n        if (left == 1 &amp;&amp; right != 1)\n            return right;\n        if (left != 1 &amp;&amp; right == 1)\n            return left;\n        if (left == 1 &amp;&amp; right == 1)\n            return 1;\n        var xInts   = left.Data;\n        var yInts   = right.Data;\n        var mulInts = new uint[Math.Max(xInts.Length, yInts.Length) &lt;&lt; 1];\n        for (var i = 0; i &lt; xInts.Length; i++)\n        {\n            var   index     = i;\n            ulong remainder = 0;\n            foreach (var yi in yInts)\n            {\n                remainder        = remainder + (ulong)xInts[i] * yi + mulInts[index];\n                mulInts[index++] = (uint)remainder;\n                remainder        = remainder >> 32;\n            }\n            while (remainder != 0)\n            {\n                remainder        += mulInts[index];\n                mulInts[index++] =  (uint)remainder;\n                remainder        =  remainder >> 32;\n            }\n        }\n        return new xIntX(left.Sign * right.Sign, mulInts, DataBitWidth, _unsigned);\n    }\n    public static xIntX operator *(xIntX left, xIntX right)\n    {\n        return Multiply(left, right);\n    }\n    public static xIntX operator >> (xIntX value, int shift)\n    {\n        if (shift == 0)\n            return value;\n        if (shift == int.MinValue)\n            return value &lt;&lt; int.MaxValue &lt;&lt; 1;\n        if (shift &lt; 0)\n            return value &lt;&lt; -shift;\n        var xd          = value.Data;\n        var shiftAmount = 32;\n        var invShift    = 0;\n        var bufLen      = xd.Length;\n        while (bufLen > 1 &amp;&amp; xd[bufLen - 1] == 0)\n            bufLen--;\n        for (var count = shift; count > 0; count -= shiftAmount)\n        {\n            if (count &lt; shiftAmount)\n            {\n                shiftAmount = count;\n                invShift    = 32 - shiftAmount;\n            }\n            ulong carry = 0;\n            for (var i = bufLen - 1; i >= 0; i--)\n            {\n                var val = (ulong)xd[i] >> shiftAmount;\n                val   |= carry;\n                carry =  (ulong)xd[i] &lt;&lt; invShift;\n                xd[i] =  (uint)val;\n            }\n        }\n        return new xIntX(value.Sign, xd, DataBitWidth, _unsigned);\n    }\n    public static xIntX operator &lt;&lt;(xIntX value, int shift)\n    {\n        if (shift == 0)\n            return value;\n        if (shift == int.MinValue)\n            return value >> int.MaxValue >> 1;\n        if (shift &lt; 0)\n            return value >> -shift;\n        var digitShift = shift \/ 32;\n        var smallShift = shift - digitShift * 32;\n        var xd         = value.Data;\n        var xl         = xd.Length;\n        var zd         = new uint[xl + digitShift + 1];\n        if (smallShift == 0)\n        {\n            for (var index = 0; index &lt; xl; ++index)\n                zd[index + digitShift] = xd[index];\n        }\n        else\n        {\n            var  carryShift = 32 - smallShift;\n            uint carry      = 0;\n            int  index;\n            for (index = 0; index &lt; xl; ++index)\n            {\n                var rot = xd[index];\n                zd[index + digitShift] = (rot &lt;&lt; smallShift) | carry;\n                carry                  = rot >> carryShift;\n            }\n            zd[index + digitShift] = carry;\n        }\n        return new xIntX(value.Sign, zd, DataBitWidth, _unsigned);\n    }\n    public static xIntX operator |(xIntX left, xIntX right)\n    {\n        if (left == 0)\n            return right;\n        if (right == 0)\n            return left;\n        var z    = new uint[Math.Max(left.Data.Length, right.Data.Length)];\n        var lExt = left.Sign  &lt; 0 ? uint.MaxValue : 0U;\n        var rExt = right.Sign &lt; 0 ? uint.MaxValue : 0U;\n        for (var i = 0; i &lt; z.Length; i++)\n        {\n            var xu = i &lt; left.Data.Length ? left.Data[i] : lExt;\n            var yu = i &lt; right.Data.Length ? right.Data[i] : rExt;\n            z[i] = xu | yu;\n        }\n        return new xIntX(left.Sign * right.Sign, z, DataBitWidth, _unsigned);\n    }\n    public static xIntX operator ^(xIntX left, xIntX right)\n    {\n        var z    = new uint[Math.Max(left.Data.Length, right.Data.Length)];\n        var lExt = left.Sign  &lt; 0 ? uint.MaxValue : 0U;\n        var rExt = right.Sign &lt; 0 ? uint.MaxValue : 0U;\n        for (var i = 0; i &lt; z.Length; i++)\n        {\n            var xu = i &lt; left.Data.Length ? left.Data[i] : lExt;\n            var yu = i &lt; right.Data.Length ? right.Data[i] : rExt;\n            z[i] = xu ^ yu;\n        }\n        return new xIntX(left.Sign * right.Sign, z, DataBitWidth, _unsigned);\n    }\n    public static xIntX operator &amp;(xIntX left, xIntX right)\n    {\n        if (left == 0 || right == 0)\n            return 0;\n        var z    = new uint[Math.Max(left.Data.Length, right.Data.Length)];\n        var lExt = left.Sign  &lt; 0 ? uint.MaxValue : 0U;\n        var rExt = right.Sign &lt; 0 ? uint.MaxValue : 0U;\n        for (var i = 0; i &lt; z.Length; i++)\n        {\n            var xu = i &lt; left.Data.Length ? left.Data[i] : lExt;\n            var yu = i &lt; right.Data.Length ? right.Data[i] : rExt;\n            z[i] = xu &amp; yu;\n        }\n        return new xIntX(left.Sign * right.Sign, z, DataBitWidth, _unsigned);\n    }\n    public string ToBinaryString()\n    {\n        var bytes  = ToByteArray();\n        var index  = bytes.Length - 1;\n        var base2  = new StringBuilder(bytes.Length * 8);\n        var binary = Convert.ToString(bytes[index], 2);\n        if (binary[0] != '0' &amp;&amp; Sign == 1) base2.Append('0');\n        base2.Append(binary);\n        for (index--; index >= 0; index--)\n            base2.Append(Convert.ToString(bytes[index], 2).PadLeft(8, '0'));\n        return base2.ToString();\n    }\n    public string ToOctalString()\n    {\n        var bytes         = ToByteArray();\n        var index         = bytes.Length - 1;\n        var base8         = new StringBuilder((bytes.Length \/ 3 + 1) * 8);\n        var rem           = bytes.Length % 3;\n        if (rem == 0) rem = 3;\n        var base0         = 0;\n        while (rem != 0)\n        {\n            base0 &lt;&lt;= 8;\n            base0 +=  bytes[index--];\n            rem--;\n        }\n        var octal = Convert.ToString(base0, 8);\n        if (octal[0] != '0' &amp;&amp; Sign == 1) base8.Append('0');\n        base8.Append(octal);\n        while (index >= 0)\n        {\n            base0 = (bytes[index] &lt;&lt; 16) + (bytes[index - 1] &lt;&lt; 8) + bytes[index - 2];\n            base8.Append(Convert.ToString(base0, 8).PadLeft(8, '0'));\n            index -= 3;\n        }\n        return base8.ToString();\n    }\n    public static xIntX Pow(xIntX value, xIntX exponent, int bitLength)\n    {\n        if (value == null)\n            throw new ArgumentNullException(\"Value cannot be null\");\n        if (exponent == null)\n            throw new ArgumentNullException(\"Exponent cannot be null\");\n        if (exponent &lt; 0)\n            throw new ArgumentOutOfRangeException(\"Exponent\", \"Exponent cannot be negative\");\n        var result = new xIntX(\"1\", bitLength, _unsigned);\n        while (exponent != 0)\n        {\n            if ((exponent &amp; 1) != 0)\n                result *= value;\n            exponent >>= 1;\n            value    *=  value;\n        }\n        return result;\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Works well, not as good as BigInteger\n    \/\/\/ &lt;\/summary>\n    public static BigInteger ModPow(BigInteger n, BigInteger e, BigInteger m)\n    {\n        var n1 = n;\n        var e1 = e;\n        if (e1 == 0)\n            return 1;\n        if (e1 == 1)\n            return n1 % m;\n        if (e1 == 2)\n            return n1 * n1 % m;\n        n1 %= m;\n        BigInteger r = 1;\n        if ((e1 &amp; 1) == 1)\n            r = n1;\n        while (e1 > 1)\n        {\n            e1 >>= 1;\n            n1 =   n1 * n1 % m;\n            if ((e1 &amp; 1) == 1)\n                r = r * n1 % m;\n        }\n        return r;\n    }\n    public static int GetSign(uint[] value)\n    {\n        var allZero = true;\n        for (var i = 0; i &lt; value.Length; i++)\n            if (value[i] != 0)\n            {\n                allZero = false;\n                break;\n            }\n        if (allZero)\n            return 0;\n        return (value[value.Length - 1] &amp; HiNeg) == 0 ? 1 : -1;\n    }\n    private static int GetDataUsed(uint[] array)\n    {\n        var neg      = GetSign(array) &lt; 0;\n        var dataUsed = array.Length;\n        if (!neg)\n        {\n            while (dataUsed > 1 &amp;&amp; array[dataUsed - 1] == 0)\n                --dataUsed;\n            if (dataUsed == 0)\n                dataUsed = 1;\n        }\n        return dataUsed;\n    }\n    public int GetDecimalPlaces(xIntX a)\n    {\n        var dPlaces = 0;\n        if (a.Sign == 0)\n            return 1;\n        if (a.Sign &lt; 0)\n            try\n            {\n                a = -a;\n            }\n            catch (Exception ex)\n            {\n                return 0;\n            }\n        var biRadix = new xIntX(10, DataBitWidth, _unsigned);\n        while (a > 0)\n            try\n            {\n                Divide(a, biRadix, out var remainder, out var quotient);\n                a = quotient;\n                dPlaces++;\n            }\n            catch (Exception ex)\n            {\n                break;\n            }\n        return dPlaces;\n    }\n    private uint[] TwosComplement(uint[] d)\n    {\n        var  i = 0;\n        uint v = 0;\n        for (; i &lt; d.Length; i++)\n        {\n            v    = ~d[i] + 1;\n            d[i] = v;\n            if (v != 0)\n            {\n                i++;\n                break;\n            }\n        }\n        if (v != 0)\n        {\n            for (; i &lt; d.Length; i++)\n                d[i] = ~d[i];\n        }\n        else\n        {\n            Array.Resize(ref d, d.Length + 1);\n            d[d.Length - 1] = 1;\n        }\n        return d;\n    }\n    public (xIntX approximateRoot, BigRational realRoot) Sqrt()\n    {\n        var n = (BigRational)this;\n        var r = n.Sqrt();\n        return (r.WholePart, r);\n    }\n    public xIntX Pow(int e)\n    {\n        var ans = this;\n        if (e == 1)\n            return ans;\n        if (e == 0)\n            return 1;\n        for (var i = 1; i != e; i++)\n            ans *= this;\n        return ans;\n    }\n    public static double Log(xIntX value, double baseValue)\n    {\n        var c          = 0.0;\n        var d          = 0.5;\n        var dataLength = value.DataUsed;\n        var topBits    = 0;\n        var x          = value.Data[dataLength - 1];\n        while (x > 0)\n        {\n            x >>= 1;\n            topBits++;\n        }\n        var bitLength = (dataLength - 1) * 32 + topBits;\n        var bit       = (uint)(1 &lt;&lt; (topBits - 1));\n        for (var index = dataLength - 1; index >= 0; --index)\n        {\n            for (; bit != 0U; bit >>= 1)\n            {\n                if (((int)value.Data[index] &amp; (int)bit) != 0)\n                    c += d;\n                d *= 0.5;\n            }\n            bit = 2147483648U;\n        }\n        return (Math.Log(c) + 0.69314718055994530941723212145818 * bitLength) \/ Math.Log(baseValue);\n    }\n    public static List&lt;xIntX> GetFactors(xIntX n)\n    {\n        var Factors = new List&lt;xIntX>();\n        var s       = (xIntX)1 &lt;&lt; ((int)Math.Ceiling(Log(n, 2)) >> 1);\n        var a       = (xIntX)3;\n        while (a &lt; s)\n        {\n            if (n % a == 0)\n            {\n                Factors.Add(a);\n                if (a * a != n)\n                    Factors.Add(n \/ a);\n            }\n            a += 2;\n        }\n        return Factors;\n    }\n    public static xIntX GreatestCommonDivisor(xIntX a, xIntX b)\n    {\n        while (b > 0)\n        {\n            var r = a % b;\n            a = b;\n            b = r;\n        }\n        return a;\n    }\n    public static xIntX LeastCommonMultiple(xIntX a, xIntX b)\n    {\n        return a * b \/ a.Gcd(b);\n    }\n    public static double Log10(xIntX value)\n    {\n        return Log(value, 10.0);\n    }\n    public static double LogN(xIntX value)\n    {\n        return Log(value, 2.0);\n    }\n    public void ConstructFromArray(byte[] value, int bitLength)\n    {\n        var minSize = value.Length \/ DataSize;\n        if (value == null)\n            throw new ArgumentNullException(\"value\");\n        DataBitWidth = bitLength;\n        DataLength   = DataBitWidth >> 5;\n        var byteCount      = value.Length;\n        var isNegative     = _unsigned == false &amp;&amp; byteCount > 0 &amp;&amp; (value[byteCount - 1] &amp; 0x80) == 0x80;\n        var unalignedBytes = byteCount % DataSize;\n        var dwordCount     = byteCount \/ DataSize + (unalignedBytes == 0 ? 0 : 1);\n        Data = new uint[Math.Max(dwordCount, DataLength)];\n        if (byteCount == 0)\n            return;\n        int curDword, curByte, byteInDword;\n        curByte = 3;\n        for (curDword = 0; curDword &lt; dwordCount - (unalignedBytes == 0 ? 0 : 1); curDword++)\n        {\n            byteInDword = 0;\n            while (byteInDword &lt; DataSize)\n            {\n                Data[curDword] &lt;&lt;= 8;\n                Data[curDword] |=  value[curByte];\n                curByte--;\n                byteInDword++;\n            }\n            curByte += 8;\n        }\n        if (unalignedBytes != 0)\n        {\n            if (isNegative)\n                Data[dwordCount - 1] = 0xffffffff;\n            for (curByte = byteCount - 1; curByte >= byteCount - unalignedBytes; curByte--)\n            {\n                Data[curDword] &lt;&lt;= 8;\n                Data[curDword] |=  value[curByte];\n            }\n        }\n    }\n    private class xIntXConverter : TypeConverter\n    {\n        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)\n        {\n            return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);\n        }\n        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)\n        {\n            if (value != null)\n                if (TryParse($\"{value}\", out var i))\n                    return i;\n            return new xIntX(0, DataBitWidth, _unsigned);\n        }\n        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)\n        {\n            return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);\n        }\n        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)\n        {\n            return destinationType == typeof(string) ? $\"{value}\" : base.ConvertTo(context, culture, value, destinationType);\n        }\n    }\n}\npublic class xIntXComparer : IComparer&lt;xIntX>\n{\n    public int Compare(xIntX left, xIntX right)\n    {\n        return left.CompareTo(right);\n    }\n    public bool Equals(xIntX left, xIntX right)\n    {\n        if (left == null || right == null)\n            return false;\n        return left.Equals(right);\n    }\n    public int GetHashCode(xIntX obj)\n    {\n        return obj.GetHashCode();\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Adjustable Bit Width Big Unsigned or Signed Integer 32,64,128,256,512,1024,2048\u2026 Updated: Jun-11,2021<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[121,14,120,128,119],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/263"}],"collection":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":8,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":434,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/263\/revisions\/434"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}