{"id":246,"date":"2020-12-02T10:25:06","date_gmt":"2020-12-02T10:25:06","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=246"},"modified":"2021-06-12T03:05:48","modified_gmt":"2021-06-12T03:05:48","slug":"intx-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/12\/02\/intx-cs\/","title":{"rendered":"(Obsolete) IntX.cs"},"content":{"rendered":"\n<p><strong>Adjustable Bit Width Integer 32,64,128,256,512,1024,2048\u2026<\/strong><\/p>\n\n\n\n<p>Why not just use BigInteger? Think Hashing which relies on overflow.<\/p>\n\n\n\n<p>Dec-03, 2020: Added ToBinaryString, ToOctalString, Pow, ModPow, GetSign, GetDataUsed, UsedData, BitWidth, GetDecimalPlaces, TwosComplement, Pow, Log, GetFactors, GreatestCommonDivisor, LeastCommonMultiple, Log10, LogN, Sqrt, and ConstructFromArray.<\/p>\n\n\n\n<p>Jun-11,2021: Obsolete Use xIntX Instead.<\/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.Runtime.Serialization;\nusing System.Text;\n[Serializable]\n[StructLayout(LayoutKind.Sequential, Pack = 1)]\n[TypeConverter(typeof(IntXConverter))]\n[DebuggerDisplay(\"{DDisplay}\")]\npublic class IntX : IComparable&lt;IntX>, IComparable, IEquatable&lt;IntX>, IConvertible, IFormattable, ISerializable\n{\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    public static readonly IntX              One   = new IntX(1,  32);\n    public static readonly IntX              Two   = new IntX(2,  32);\n    public static readonly IntX              Zero  = new IntX(0,  32);\n    public static readonly IntX              Ten   = new IntX(10, 32);\n    public static readonly IntX              Three = new IntX(3,  32);\n    private readonly       SerializationInfo SInfo;\n    public                 uint[]            Data;\n    public IntX(IntX value, int bitLength)\n    {\n        DataBitWidth = bitLength;\n        DataLength   = DataBitWidth >> 5;\n        Data         = new uint[DataLength];\n        value.Data.CopyTo(Data, 0);\n    }\n    public IntX(string value, int bitLength)\n    {\n        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 IntX(byte value, int bitLength)\n    {\n        DataBitWidth = bitLength;\n        DataLength   = DataBitWidth >> 5;\n        Data         = new uint[DataLength];\n        Data[0]      = value;\n    }\n    public IntX(bool value, int bitLength)\n    {\n        DataBitWidth = bitLength;\n        DataLength   = DataBitWidth >> 5;\n        Data         = new uint[DataLength];\n        Data[0]      = (uint) (value ? 1 : 0);\n    }\n    public IntX(char value, int bitLength)\n    {\n        DataLength = DataBitWidth >> 5;\n        Data       = new uint[DataLength];\n        Data[0]    = value;\n    }\n    public IntX(BigDecimal value, int bitLength)\n    {\n        var ba = value.WholePart.ToByteArray();\n        DataBitWidth = bitLength;\n        DataLength   = DataBitWidth >> 5;\n        var len = ba.Length \/ DataSize;\n        Data = new uint[DataLength];\n        for (var i = 0; i &lt; Data.Length; i++)\n            Data[i] = BitConverter.ToUInt32(ba, i * DataSize);\n    }\n    public IntX(decimal value, int bitLength)\n    {\n        DataBitWidth = bitLength;\n        DataLength   = DataBitWidth >> 5;\n        Data         = new uint[DataLength];\n        if (value &lt; 0)\n        {\n            var n = -new IntX(-value, DataBitWidth);\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 IntX(double value, int bitLength) : this((decimal) value, bitLength)\n    {\n    }\n    public IntX(float value, int bitLength) : this((decimal) value, bitLength)\n    {\n    }\n    public IntX(short value, int bitLength)\n    {\n        DataBitWidth = bitLength;\n        DataLength   = DataBitWidth >> 5;\n        Data         = new uint[DataLength];\n        if (value &lt; 0)\n        {\n            var n = -new IntX(-(value + 1), DataBitWidth) - 1;\n            n.Data.CopyTo(Data, 0);\n            return;\n        }\n        Data[0] = (uint) value;\n    }\n    public IntX(int value, int bitLength)\n    {\n        DataBitWidth = bitLength;\n        DataLength   = DataBitWidth >> 5;\n        Data         = new uint[DataLength];\n        if (value &lt; 0)\n        {\n            var n = -new IntX(-(value + 1), DataBitWidth) - 1;\n            n.Data.CopyTo(Data, 0);\n            return;\n        }\n        Data[0] = (uint) value;\n    }\n    public IntX(long value, int bitLength)\n    {\n        DataBitWidth = bitLength;\n        DataLength   = DataBitWidth >> 5;\n        Data         = new uint[DataLength];\n        if (value &lt; 0)\n        {\n            var n = -new IntX(-(value + 1), DataBitWidth) - 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 IntX(sbyte value, int bitLength)\n    {\n        DataBitWidth = bitLength;\n        DataLength   = DataBitWidth >> 5;\n        Data         = new uint[DataLength];\n        if (value &lt; 0)\n        {\n            var n = -new IntX(-(value + 1), DataBitWidth) - 1;\n            n.Data.CopyTo(Data, 0);\n            return;\n        }\n        Data[0] = (uint) value;\n    }\n    public IntX(BigRational value, int bitLength)\n    {\n        value.Round(1);\n        ConstructFromArray(value.Numerator.ToByteArray(), bitLength);\n    }\n    public IntX(ushort value, int bitLength)\n    {\n        DataBitWidth = bitLength;\n        DataLength   = DataBitWidth >> 5;\n        Data         = new uint[DataLength];\n        Data[0]      = value;\n    }\n    public IntX(uint value, int bitLength)\n    {\n        DataBitWidth = bitLength;\n        DataLength   = DataBitWidth >> 5;\n        Data         = new uint[DataLength];\n        Data[0]      = value;\n    }\n    public IntX(ulong value, int bitLength)\n    {\n        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 IntX(BigInteger value, int bitLength) : this(value.ToByteArray(), bitLength)\n    {\n    }\n    public IntX(Guid value, int bitLength) : this(value.ToByteArray(), bitLength)\n    {\n    }\n    public IntX(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     = 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 IntX(int sign, uint[] array, int bitLength)\n    {\n        if (array == null)\n            throw new Exception(\"Array cannot be null.\");\n        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 (sign &lt; 0)\n            Data[DataLength - 1] |= HiNeg;\n        else\n            Data[DataLength - 1] &amp;= ~HiNeg;\n    }\n    public IntX(uint[] array, int bitLength)\n    {\n        if (array == null)\n            throw new Exception(\"Array cannot be null.\");\n        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    protected IntX(SerializationInfo info, StreamingContext context)\n    {\n        SInfo = info;\n    }\n    private static int DataBitWidth\n    {\n        get => _dataBitWidth;\n        set\n        {\n            if (value &lt; 32)\n                throw new Exception($\"Data Bit Width {value} must not be less than 32.\");\n            _dataBitWidth = value;\n        }\n    }\n    [DebuggerBrowsable(DebuggerBrowsableState.Never)]\n    private string DDisplay => ToString();\n    public IntX MaxValue\n    {\n        get\n        {\n            var r = new IntX(0, DataBitWidth);\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            IntX bw = 1;\n            var  v  = new IntX(this, DataBitWidth);\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 int Sign\n    {\n        get\n        {\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(IntX 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(IntX 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    public void GetObjectData(SerializationInfo info, StreamingContext context)\n    {\n        info.AddValue(\"Bits\", DataBitWidth);\n        info.AddValue(\"Data\", Data, typeof(uint[]));\n    }\n    public void OnDeserialization(object sender)\n    {\n        if (SInfo == null)\n            return;\n        DataBitWidth = SInfo.GetInt32(\"Bits\");\n        if (DataBitWidth != 0)\n        {\n            DataLength = DataBitWidth >> 5;\n            var array = (uint[]) SInfo.GetValue(\"Data\", typeof(uint[]));\n            if (array == null)\n                throw new Exception(\"Array cannot be null.\");\n            if (array.Length != DataLength)\n                Array.Resize(ref array, DataLength);\n            Data = new uint[DataLength];\n            var ba = new byte[4];\n            for (var i = 0; i &lt; DataLength; i++)\n            {\n                Array.Copy(BitConverter.GetBytes(array[i]), 0, ba, 0, DataSize);\n                Data[i] = BitConverter.ToUInt32(ba, 0);\n            }\n        }\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        var s    = Sign;\n        var hash = ((s &lt;&lt; 7) | (s >> 25)) ^ 0x811c9dc5;\n        for (var i = 0; i &lt; Data.Length; i++)\n        {\n            hash ^= ((hash &lt;&lt; 7) | (hash >> 25)) ^ Data[i];\n            hash *= 0x1000193;\n        }\n        return (int) hash;\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 IntX(this, DataBitWidth);\n        if (negative)\n            try\n            {\n                a = -a;\n            }\n            catch (Exception ex)\n            {\n            }\n        var          biRadix = new IntX(radix, DataBitWidth);\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 IntX Abs(IntX 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 IntX Parse(string value)\n    {\n        return Parse(value, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);\n    }\n    public static IntX Parse(string value, NumberStyles style)\n    {\n        return Parse(value, style, NumberFormatInfo.CurrentInfo);\n    }\n    public static IntX Parse(string value, IFormatProvider provider)\n    {\n        return Parse(value, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));\n    }\n    public static IntX 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 IntX 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 IntX 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 IntX result)\n    {\n        result = new IntX(0, DataBitWidth);\n        if (digits == null)\n            return false;\n        var multiplier = new IntX(1, DataBitWidth);\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 >= '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] == '-')\n            result = -result;\n        return true;\n    }\n    public static int Compare(IntX left, object right)\n    {\n        if (right is IntX)\n            return Compare(left, (IntX) right);\n        if (right is bool)\n            return Compare(left, new IntX((bool) right, DataBitWidth));\n        if (right is byte)\n            return Compare(left, new IntX((byte) right, DataBitWidth));\n        if (right is char)\n            return Compare(left, new IntX((char) right, DataBitWidth));\n        if (right is decimal)\n            return Compare(left, new IntX((decimal) right, DataBitWidth));\n        if (right is double)\n            return Compare(left, new IntX((double) right, DataBitWidth));\n        if (right is short)\n            return Compare(left, new IntX((short) right, DataBitWidth));\n        if (right is int)\n            return Compare(left, new IntX((int) right, DataBitWidth));\n        if (right is long)\n            return Compare(left, new IntX((long) right, DataBitWidth));\n        if (right is sbyte)\n            return Compare(left, new IntX((sbyte) right, DataBitWidth));\n        if (right is float)\n            return Compare(left, new IntX((float) right, DataBitWidth));\n        if (right is ushort)\n            return Compare(left, new IntX((ushort) right, DataBitWidth));\n        if (right is uint)\n            return Compare(left, new IntX((uint) right, DataBitWidth));\n        if (right is ulong)\n            return Compare(left, new IntX((ulong) right, DataBitWidth));\n        var bytes = right as byte[];\n        if (bytes != null)\n            return Compare(left, new IntX(bytes, DataBitWidth));\n        if (right is Guid)\n            return Compare(left, new IntX((Guid) right, DataBitWidth));\n        throw new ArgumentException();\n    }\n    public static int Compare(IntX left, IntX 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 IntX(bool value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static implicit operator IntX(byte value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static implicit operator IntX(char value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static explicit operator IntX(decimal value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static explicit operator IntX(double value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static implicit operator IntX(short value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static implicit operator IntX(int value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static implicit operator IntX(long value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static implicit operator IntX(sbyte value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static explicit operator IntX(float value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static implicit operator IntX(ushort value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static implicit operator IntX(uint value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static implicit operator IntX(ulong value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static implicit operator IntX(BigInteger value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static implicit operator IntX(BigRational value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static implicit operator IntX(BigDecimal value)\n    {\n        return new IntX(value, DataBitWidth);\n    }\n    public static explicit operator bool(IntX value)\n    {\n        return (byte) value.Data[0] != 0;\n    }\n    public static explicit operator byte(IntX value)\n    {\n        return (byte) value.Data[0];\n    }\n    public static explicit operator char(IntX value)\n    {\n        return (char) (ushort) value.Data[0];\n    }\n    public static explicit operator decimal(IntX 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(IntX 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(IntX 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(IntX 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(IntX 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(IntX 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(IntX value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        return value.Data[0];\n    }\n    public static explicit operator ushort(IntX value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        return (ushort) value.Data[0];\n    }\n    public static explicit operator ulong(IntX 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(IntX value)\n    {\n        return new BigInteger(value.ToByteArray());\n    }\n    public static explicit operator BigRational(IntX value)\n    {\n        return new BigRational(new BigInteger(value.ToByteArray()));\n    }\n    public static bool operator >(IntX left, IntX right)\n    {\n        return left.CompareTo(right) > 0;\n    }\n    public static bool operator &lt;(IntX left, IntX right)\n    {\n        return Compare(left, right) &lt; 0;\n    }\n    public static bool operator >=(IntX left, IntX right)\n    {\n        return Compare(left, right) >= 0;\n    }\n    public static bool operator &lt;=(IntX left, IntX right)\n    {\n        return Compare(left, right) &lt;= 0;\n    }\n    public static bool operator !=(IntX left, IntX right)\n    {\n        return !left.Equals(right);\n    }\n    public static bool operator ==(IntX left, IntX right)\n    {\n        return left.Equals(right);\n    }\n    public static IntX operator +(IntX value)\n    {\n        return value;\n    }\n    public static IntX operator ~(IntX 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 IntX(da, DataBitWidth);\n    }\n    public static IntX operator -(IntX value)\n    {\n        if (ReferenceEquals(value, null))\n            throw new ArgumentNullException(\"value\");\n        if (value.IsZero)\n            return Zero;\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 IntX(da, DataBitWidth);\n    }\n    public static IntX operator ++(IntX value)\n    {\n        return value + 1;\n    }\n    public static IntX operator --(IntX value)\n    {\n        return value - 1;\n    }\n    public static IntX Negate(IntX 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 IntX(value.Sign, ldata, DataBitWidth);\n    }\n    public static IntX operator +(IntX left, IntX 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 IntX(left.Sign * right.Sign, result, DataBitWidth);\n    }\n    public static IntX operator -(IntX left, IntX 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 IntX(da, DataBitWidth);\n    }\n    public static IntX Add(IntX left, IntX right)\n    {\n        return left + right;\n    }\n    public static IntX Subtract(IntX left, IntX right)\n    {\n        return left - right;\n    }\n    public static IntX Divide(IntX dividend, IntX divisor)\n    {\n        if (divisor == 0)\n            throw new DivideByZeroException();\n        return DivRem(dividend, divisor, out var integer);\n    }\n    public static void Divide(IntX dividend, IntX divisor, out IntX remainder, out IntX 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 IntX(1,                            rem, DataBitWidth);\n        quotient  = new IntX(dividend.Sign * divisor.Sign, quo, DataBitWidth);\n    }\n    public static IntX DivRem(IntX dividend, IntX divisor, out IntX 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 IntX(1, rem, DataBitWidth);\n        return new IntX(dividend.Sign * divisor.Sign, quotient, DataBitWidth);\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 IntX Remainder(IntX dividend, IntX divisor)\n    {\n        DivRem(dividend, divisor, out var remainder);\n        return remainder;\n    }\n    public static IntX Max(IntX left, IntX right)\n    {\n        return left.CompareTo(right) &lt; 0 ? right : left;\n    }\n    public static IntX Min(IntX left, IntX right)\n    {\n        return left.CompareTo(right) &lt;= 0 ? left : right;\n    }\n    public static IntX operator %(IntX dividend, IntX divisor)\n    {\n        return Remainder(dividend, divisor);\n    }\n    public static IntX operator \/(IntX dividend, IntX 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 IntX Multiply(IntX left, IntX right)\n    {\n        if (left == 0 || right == 0)\n            return Zero;\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 One;\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 IntX(left.Sign * right.Sign, mulInts, DataBitWidth);\n    }\n    public static IntX operator *(IntX left, IntX right)\n    {\n        return Multiply(left, right);\n    }\n    public static IntX operator >>(IntX 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 IntX(value.Sign, xd, DataBitWidth);\n    }\n    public static IntX operator &lt;&lt;(IntX 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 IntX(value.Sign, zd, DataBitWidth);\n    }\n    public static IntX operator |(IntX left, IntX 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 IntX(left.Sign * right.Sign, z, DataBitWidth);\n    }\n    public static IntX operator ^(IntX left, IntX 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 IntX(left.Sign * right.Sign, z, DataBitWidth);\n    }\n    public static IntX operator &amp;(IntX left, IntX 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 IntX(left.Sign * right.Sign, z, DataBitWidth);\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 IntX Pow(IntX value, IntX 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 IntX(\"1\", bitLength);\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    public static IntX ModPow(IntX n, IntX e, IntX m)\n    {\n        var n1 = new IntX(n, DataBitWidth);\n        var e1 = new IntX(e, DataBitWidth);\n        var r  = new IntX(1, DataBitWidth);\n        while (e1 != 0)\n        {\n            if (e1 % 2 == 1)\n                r = r * n1 % m;\n            e1 >>= 1;\n            n1 =   n1 * 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()\n    {\n        var dPlaces = 0;\n        if (Sign == 0)\n            return 1;\n        var a = new IntX(this, DataBitWidth);\n        if (Sign &lt; 0)\n            try\n            {\n                a = -a;\n            }\n            catch (Exception ex)\n            {\n                return 0;\n            }\n        var biRadix = new IntX(10, DataBitWidth);\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    public (IntX approximateRoot, BigRational realRoot) Sqrt()\n    {\n        var n = (BigRational)this;\n        var r = n.Sqrt();\n        return (r.GetWholePart, r);\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 IntX 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(IntX 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;IntX> GetFactors(IntX n)\n    {\n        var Factors = new List&lt;IntX>();\n        var s       = (IntX) 1 &lt;&lt; ((int) Math.Ceiling(Log(n, 2)) >> 1);\n        var a       = Three;\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 IntX GreatestCommonDivisor(IntX a, IntX 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 IntX LeastCommonMultiple(IntX a, IntX b)\n    {\n        return a * b \/ a.Gcd(b);\n    }\n    public static double Log10(IntX value)\n    {\n        return Log(value, 10.0);\n    }\n    public static double LogN(IntX 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 = 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 IntXConverter : 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 IntX(0, DataBitWidth);\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 IntXComparer : IComparer&lt;IntX>\n{\n    public int Compare(IntX left, IntX right)\n    {\n        return left.CompareTo(right);\n    }\n    public bool Equals(IntX left, IntX right)\n    {\n        if (left == null || right == null)\n            return false;\n        return left.Equals(right);\n    }\n    public int GetHashCode(IntX obj)\n    {\n        return obj.GetHashCode();\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Adjustable Bit Width Integer 32,64,128,256,512,1024,2048\u2026 Why not just use BigInteger? Think Hashing which relies on overflow. Dec-03, 2020: Added ToBinaryString, ToOctalString, Pow, ModPow, GetSign, GetDataUsed, UsedData, BitWidth, GetDecimalPlaces, TwosComplement, Pow, Log, GetFactors, GreatestCommonDivisor, LeastCommonMultiple, Log10, LogN, Sqrt, and ConstructFromArray. Jun-11,2021: Obsolete Use xIntX Instead.<\/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,118],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/246"}],"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=246"}],"version-history":[{"count":11,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/246\/revisions"}],"predecessor-version":[{"id":440,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/246\/revisions\/440"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}