{"id":179,"date":"2020-08-11T03:12:07","date_gmt":"2020-08-11T03:12:07","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=179"},"modified":"2021-06-11T20:20:03","modified_gmt":"2021-06-11T20:20:03","slug":"longlong-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/08\/11\/longlong-cs\/","title":{"rendered":"LongLong.cs"},"content":{"rendered":"\n<p>Int64 Bit Class<\/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.ComponentModel;\nusing System.Diagnostics;\nusing System.Globalization;\nusing System.Linq;\nusing System.Numerics;\nusing System.Runtime.InteropServices;\nusing System.Text;\n\/\/\/ &lt;summary>\n\/\/\/     Used to verify correct mathematical operations before extending to 128, 256, 512, 1024, and 2048 versions\n\/\/\/ &lt;\/summary>\n[Serializable]\n[StructLayout(LayoutKind.Sequential, Pack = 1)]\n[TypeConverter(typeof(LongLongConverter))]\n[DebuggerDisplay(\"{DDisplay}\")]\npublic struct LongLong : IComparable&lt;LongLong>, IComparable, IEquatable&lt;LongLong>, IConvertible, IFormattable\n{\n    private ulong B64;\n    [DebuggerBrowsable(DebuggerBrowsableState.Never)]\n    private string DDisplay => ToString();\n    public static LongLong Zero     = new LongLong(0);\n    public static LongLong Ten      = new LongLong(10);\n    public static LongLong One      = new LongLong(1);\n    public static LongLong MaxValue = GetMaxValue();\n    public static LongLong MinValue = GetMinValue();\n    private static LongLong GetMaxValue()\n    {\n        return new LongLong(ulong.MaxValue);\n    }\n    private static LongLong GetMinValue()\n    {\n        return new LongLong(0);\n    }\n    public LongLong(LongLong value)\n    {\n        B64 = value.B64;\n    }\n    public LongLong(string value)\n    {\n        if (!TryParse(value, out var result))\n            throw new Exception(\"TryParse Failed.\");\n        B64 = result.B64;\n    }\n    public LongLong(byte value)\n    {\n        B64 = value;\n    }\n    public LongLong(bool value)\n    {\n        B64 = (ulong) (value ? 1 : 0);\n    }\n    public LongLong(char value)\n    {\n        B64 = value;\n    }\n    public LongLong(decimal value)\n    {\n        if (value &lt; 0)\n            throw new Exception(\"Value must be a positive.\");\n        var bits = decimal.GetBits(value);\n        B64 = (uint) bits[0];\n        if (value &lt; 0)\n            B64 = ~B64;\n    }\n    public LongLong(double value) : this((decimal) value)\n    {\n    }\n    public LongLong(float value) : this((decimal) value)\n    {\n    }\n    public LongLong(short value)\n    {\n        if (value &lt; 0)\n            throw new Exception(\"Value must be a positive.\");\n        B64 = (ulong) value;\n    }\n    public LongLong(int value)\n    {\n        if (value &lt; 0)\n            throw new Exception(\"Value must be a positive.\");\n        B64 = (ulong) value;\n    }\n    public LongLong(long value)\n    {\n        if (value &lt; 0)\n            throw new Exception(\"Value must be a positive.\");\n        B64 = (ulong) value;\n    }\n    public LongLong(UInt128 value)\n    {\n        if (value &lt; 0)\n            throw new Exception(\"Value must be a positive.\");\n        B64 = value.B64;\n    }\n    public LongLong(sbyte value)\n    {\n        if (value &lt; 0)\n            throw new Exception(\"Value must be a positive.\");\n        B64 = (ulong) value;\n    }\n    public LongLong(ushort value)\n    {\n        B64 = value;\n    }\n    public LongLong(uint value)\n    {\n        B64 = value;\n    }\n    public LongLong(ulong value)\n    {\n        B64 = value;\n    }\n    public LongLong(Guid value) : this(value.ToByteArray())\n    {\n    }\n    public LongLong(byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        if (value.Length != 64)\n            throw new Exception(\"Values length must be 64 bytes.\");\n        B64 = BitConverter.ToUInt64(value, 0);\n    }\n    \/\/public LongLong(ulong b64)\n    \/\/{\n    \/\/    B64 = b64;\n    \/\/}\n    public LongLong(uint[] array)\n    {\n        if (array == null)\n            throw new Exception(\"Array cannot be null.\");\n        var b64 = new byte[8];\n        if (array.Length > 0)\n        {\n            Array.Copy(BitConverter.GetBytes(array[0]), 0, b64, 0, 4);\n            if (array.Length > 1)\n                Array.Copy(BitConverter.GetBytes(array[1]), 0, b64, 4, 4);\n        }\n        B64 = BitConverter.ToUInt64(b64, 0);\n    }\n    public int BitWidth\n    {\n        get\n        {\n            LongLong bitWidth = 1;\n            var      v        = this;\n            while ((v >>= 1) > 0)\n                bitWidth++;\n            if (bitWidth &lt; 8)\n                bitWidth = 8;\n            while (bitWidth % 8 != 0)\n                bitWidth++;\n            return (int) bitWidth;\n        }\n    }\n    public override int GetHashCode()\n    {\n        return B64.GetHashCode();\n    }\n    public override bool Equals(object obj)\n    {\n        return base.Equals(obj);\n    }\n    public bool Equals(LongLong obj)\n    {\n        return B64 == obj.B64;\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 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', min);\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)));\n    }\n    private string ToHexString(bool caps, int min)\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)\n    {\n        var buf = ToByteArray();\n        var bi  = new BigInteger(buf.Concat(new byte[] {0}).ToArray());\n        return bi.ToString();\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 (int) this;\n    }\n    public ulong ToUInt64(IFormatProvider provider)\n    {\n        return (ulong) 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 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 LongLong Parse(string value)\n    {\n        return Parse(value, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);\n    }\n    public static LongLong Parse(string value, NumberStyles style)\n    {\n        return Parse(value, style, NumberFormatInfo.CurrentInfo);\n    }\n    public static LongLong Parse(string value, IFormatProvider provider)\n    {\n        return Parse(value, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));\n    }\n    public static LongLong 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 LongLong 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 LongLong result)\n    {\n        result = Zero;\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 TryParseHex(value, out result);\n        return TryParseNum(value, out result);\n    }\n    private static bool TryParseHex(string value, out LongLong result)\n    {\n        if (value.Length > 128)\n            throw new OverflowException();\n        result = Zero;\n        var pos = 0;\n        for (var i = value.Length - 1; i >= 0; i--)\n        {\n            var   ch = value[i];\n            ulong bch;\n            if (ch >= '0' &amp;&amp; ch &lt;= '9')\n                bch = (ulong) (ch - '0');\n            else if (ch >= 'A' &amp;&amp; ch &lt;= 'F')\n                bch = (ulong) (ch - 'A' + 10);\n            else if (ch >= 'a' &amp;&amp; ch &lt;= 'f')\n                bch = (ulong) (ch - 'a' + 10);\n            else\n                return false;\n            if (pos &lt; 64)\n                result.B64 |= bch &lt;&lt; pos;\n            pos += 4;\n        }\n        return true;\n    }\n    private static bool TryParseNum(string value, out LongLong result)\n    {\n        result = Zero;\n        foreach (var ch in value)\n        {\n            byte b;\n            if (ch >= '0' &amp;&amp; ch &lt;= '9')\n                b = (byte) (ch - '0');\n            else\n                return false;\n            result =  Ten * result;\n            result += b;\n        }\n        return true;\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        return Convert.ToUInt16(B64);\n    }\n    uint IConvertible.ToUInt32(IFormatProvider provider)\n    {\n        return Convert.ToUInt32(B64);\n    }\n    ulong IConvertible.ToUInt64(IFormatProvider provider)\n    {\n        return B64;\n    }\n    int IComparable.CompareTo(object obj)\n    {\n        return Compare(this, obj);\n    }\n    public static int Compare(LongLong left, object right)\n    {\n        if (right is LongLong)\n            return Compare(left, (LongLong) right);\n        if (right is bool)\n            return Compare(left, new LongLong((bool) right));\n        if (right is byte)\n            return Compare(left, new LongLong((byte) right));\n        if (right is char)\n            return Compare(left, new LongLong((char) right));\n        if (right is decimal)\n            return Compare(left, new LongLong((decimal) right));\n        if (right is double)\n            return Compare(left, new LongLong((double) right));\n        if (right is short)\n            return Compare(left, new LongLong((short) right));\n        if (right is int)\n            return Compare(left, new LongLong((int) right));\n        if (right is long)\n            return Compare(left, new LongLong((long) right));\n        if (right is sbyte)\n            return Compare(left, new LongLong((sbyte) right));\n        if (right is float)\n            return Compare(left, new LongLong((float) right));\n        if (right is ushort)\n            return Compare(left, new LongLong((ushort) right));\n        if (right is uint)\n            return Compare(left, new LongLong((uint) right));\n        if (right is ulong)\n            return Compare(left, new LongLong((ulong) right));\n        var bytes = right as byte[];\n        if (bytes != null &amp;&amp; bytes.Length != 64)\n            return Compare(left, new LongLong(bytes));\n        if (right is Guid)\n            return Compare(left, new LongLong((Guid) right));\n        throw new ArgumentException();\n    }\n    public static int Compare(LongLong left, LongLong right)\n    {\n        return left.B64.CompareTo(right.B64);\n    }\n    public int CompareTo(LongLong value)\n    {\n        return Compare(this, value);\n    }\n    public static implicit operator LongLong(bool value)\n    {\n        return new LongLong(value);\n    }\n    public static implicit operator LongLong(byte value)\n    {\n        return new LongLong(value);\n    }\n    public static implicit operator LongLong(char value)\n    {\n        return new LongLong(value);\n    }\n    public static explicit operator LongLong(decimal value)\n    {\n        return new LongLong(value);\n    }\n    public static explicit operator LongLong(double value)\n    {\n        return new LongLong(value);\n    }\n    public static implicit operator LongLong(short value)\n    {\n        return new LongLong(value);\n    }\n    public static implicit operator LongLong(int value)\n    {\n        return new LongLong(value);\n    }\n    public static implicit operator LongLong(long value)\n    {\n        return new LongLong(value);\n    }\n    public static implicit operator LongLong(sbyte value)\n    {\n        return new LongLong(value);\n    }\n    public static explicit operator LongLong(float value)\n    {\n        return new LongLong(value);\n    }\n    public static implicit operator LongLong(ushort value)\n    {\n        return new LongLong(value);\n    }\n    public static implicit operator LongLong(uint value)\n    {\n        return new LongLong(value);\n    }\n    public static implicit operator LongLong(ulong value)\n    {\n        return new LongLong(value);\n    }\n    public static implicit operator LongLong(UInt128 value)\n    {\n        return new LongLong(value);\n    }\n    public static explicit operator bool(LongLong value)\n    {\n        return (byte) value.B64 != 0;\n    }\n    public static explicit operator byte(LongLong value)\n    {\n        return (byte) value.B64;\n    }\n    public static explicit operator char(LongLong value)\n    {\n        return (char) (ushort) value.B64;\n    }\n    public static explicit operator decimal(LongLong value)\n    {\n        return new decimal((int) (value.B64 &amp; 0xFFFFFFFF), 0, 0, false, 0);\n    }\n    public static explicit operator double(LongLong value)\n    {\n        var nfi = CultureInfo.InvariantCulture.NumberFormat;\n        if (!double.TryParse(value.ToString(nfi), NumberStyles.Number, nfi, out var d))\n            throw new OverflowException();\n        return d;\n    }\n    public static explicit operator float(LongLong value)\n    {\n        var nfi = CultureInfo.InvariantCulture.NumberFormat;\n        if (!float.TryParse(value.ToString(nfi), NumberStyles.Number, nfi, out var f))\n            throw new OverflowException();\n        return f;\n    }\n    public static explicit operator short(LongLong value)\n    {\n        return (short) (int) value.B64;\n    }\n    public static explicit operator int(LongLong value)\n    {\n        return (int) value.B64;\n    }\n    public static explicit operator long(LongLong value)\n    {\n        return (long) value.B64;\n    }\n    public static explicit operator uint(LongLong value)\n    {\n        return (uint) value.B64;\n    }\n    public static explicit operator ushort(LongLong value)\n    {\n        return (ushort) value.B64;\n    }\n    public static explicit operator ulong(LongLong value)\n    {\n        return value.B64;\n    }\n    public static explicit operator BigInteger(LongLong value)\n    {\n        return new BigInteger(value.ToByteArray().Concat(new byte[] {0}).ToArray());\n    }\n    public static explicit operator UInt128(LongLong value)\n    {\n        return new UInt128(value.B64);\n    }\n    public static bool operator >(LongLong left, LongLong right)\n    {\n        return Compare(left, right) > 0;\n    }\n    public static bool operator &lt;(LongLong left, LongLong right)\n    {\n        return Compare(left, right) &lt; 0;\n    }\n    public static bool operator >=(LongLong left, LongLong right)\n    {\n        return Compare(left, right) >= 0;\n    }\n    public static bool operator &lt;=(LongLong left, LongLong right)\n    {\n        return Compare(left, right) &lt;= 0;\n    }\n    public static bool operator !=(LongLong left, LongLong right)\n    {\n        return Compare(left, right) != 0;\n    }\n    public static bool operator ==(LongLong left, LongLong right)\n    {\n        return Compare(left, right) == 0;\n    }\n    public static LongLong operator +(LongLong value)\n    {\n        return value;\n    }\n    public static LongLong operator ~(LongLong value)\n    {\n        return -(value + One);\n    }\n    public static LongLong operator -(LongLong value)\n    {\n        return Negate(value);\n    }\n    public static LongLong operator ++(LongLong value)\n    {\n        return value + 1;\n    }\n    public static LongLong operator --(LongLong value)\n    {\n        return value - 1;\n    }\n    public static LongLong Negate(LongLong value)\n    {\n        return new LongLong(~value.B64) + 1;\n    }\n    public static LongLong operator +(LongLong left, LongLong right)\n    {\n        left.B64 += right.B64;\n        return left;\n    }\n    public static LongLong operator -(LongLong left, LongLong right)\n    {\n        return left + -right;\n    }\n    public static LongLong Add(LongLong left, LongLong right)\n    {\n        return left + right;\n    }\n    public static LongLong Subtract(LongLong left, LongLong right)\n    {\n        return left - right;\n    }\n    public static LongLong Divide(LongLong dividend, LongLong divisor)\n    {\n        return DivRem(dividend, divisor, out var integer);\n    }\n    public static LongLong DivRem(LongLong dividend, LongLong divisor, out LongLong remainder)\n    {\n        if (divisor == 0)\n            throw new DivideByZeroException();\n        DivRem(dividend.ToUIn32Array(), divisor.ToUIn32Array(), out var quotient, out var rem);\n        remainder = new LongLong(rem);\n        return new LongLong(quotient);\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 (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[0];\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 LongLong Remainder(LongLong dividend, LongLong divisor)\n    {\n        DivRem(dividend, divisor, out var remainder);\n        return remainder;\n    }\n    public static LongLong Max(LongLong left, LongLong right)\n    {\n        return left.CompareTo(right) &lt; 0 ? right : left;\n    }\n    public static LongLong Min(LongLong left, LongLong right)\n    {\n        return left.CompareTo(right) &lt;= 0 ? left : right;\n    }\n    public static int GetBitWidth(LongLong n)\n    {\n        LongLong bitWidth = 1;\n        var      v        = n;\n        while ((v >>= 1) > 0)\n            bitWidth++;\n        if (bitWidth &lt; 8)\n            bitWidth = 8;\n        while (bitWidth % 8 != 0)\n            bitWidth++;\n        return (int) bitWidth;\n    }\n    public static LongLong operator %(LongLong dividend, LongLong divisor)\n    {\n        return Remainder(dividend, divisor);\n    }\n    public static LongLong operator \/(LongLong dividend, LongLong divisor)\n    {\n        return Divide(dividend, divisor);\n    }\n    public ulong[] ToUIn64Array()\n    {\n        return new[] {B64};\n    }\n    public ulong[] ToUIn64Array(byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        var al = value.Length \/ 8;\n        if (al * 8 != value.Length)\n            al++;\n        var arr = new ulong[al];\n        Buffer.BlockCopy(value, 0, arr, 0, value.Length);\n        return arr;\n    }\n    public uint[] ToUIn32Array(byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        var al = value.Length \/ 4;\n        if (al * 4 != value.Length)\n            al++;\n        var arr = new uint[al];\n        Buffer.BlockCopy(value, 0, arr, 0, value.Length);\n        return arr;\n    }\n    public uint[] ToUIn32Array(ulong[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        var arr = new uint[value.Length * 2];\n        Buffer.BlockCopy(value, 0, arr, 0, value.Length);\n        return arr;\n    }\n    public ulong[] ToUIn64Array(uint[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        var al = value.Length \/ 2;\n        if (al * 2 != value.Length)\n            al++;\n        var arr = new ulong[al];\n        Buffer.BlockCopy(value, 0, arr, 0, value.Length);\n        return arr;\n    }\n    public uint[] ToUIn32Array()\n    {\n        var uia = new uint[16];\n        var ula = ToUIn64Array();\n        Buffer.BlockCopy(ula, 0, uia, 0, 64);\n        return uia;\n    }\n    public byte[] ToByteArray()\n    {\n        var ba  = new byte[64];\n        var ula = ToUIn64Array();\n        Buffer.BlockCopy(ula, 0, ba, 0, 64);\n        return ba;\n    }\n    public static LongLong Multiply(LongLong left, LongLong right)\n    {\n        var xInts   = left.ToUIn32Array();\n        var yInts   = right.ToUIn32Array();\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 LongLong(mulInts);\n    }\n    public static LongLong operator *(LongLong left, LongLong right)\n    {\n        return Multiply(left, right);\n    }\n    public static LongLong operator >>(LongLong value, int shift)\n    {\n        var values      = value.ToUIn64Array();\n        var valueLength = sizeof(ulong) * 8;\n        var length      = values.Length;\n        shift = shift % (length * valueLength);\n        var shiftOffset = shift \/ valueLength;\n        var bshift      = shift % valueLength;\n        var shifted     = new ulong[length];\n        for (var i = 0; i &lt; length; i++)\n        {\n            var ishift = i - shiftOffset;\n            if (ishift &lt; 0)\n                continue;\n            shifted[ishift] |= values[i] >> bshift;\n            if (bshift > 0 &amp;&amp; i + 1 &lt; length)\n                shifted[ishift] |= values[i + 1] &lt;&lt; (valueLength - bshift);\n        }\n        return new LongLong(shifted[0]);\n    }\n    public static LongLong operator &lt;&lt;(LongLong value, int shift)\n    {\n        var values      = value.ToUIn64Array();\n        var valueLength = sizeof(ulong) * 8;\n        var length      = values.Length;\n        shift %= length * valueLength;\n        var shiftOffset = shift \/ valueLength;\n        var bshift      = shift % valueLength;\n        var shifted     = new ulong[length];\n        for (var i = 0; i &lt; length; i++)\n        {\n            var ishift = i + shiftOffset;\n            if (ishift >= length)\n                continue;\n            shifted[ishift] |= values[i] &lt;&lt; bshift;\n            if (bshift > 0 &amp;&amp; i - 1 >= 0)\n                shifted[ishift] |= values[i - 1] >> (valueLength - bshift);\n        }\n        return new LongLong(shifted[0]);\n    }\n    public static LongLong operator |(LongLong left, LongLong right)\n    {\n        if (left == 0)\n            return right;\n        if (right == 0)\n            return left;\n        var x = left.ToUIn32Array();\n        var y = right.ToUIn32Array();\n        var z = new uint[Math.Max(x.Length, y.Length)];\n        for (var i = 0; i &lt; z.Length; i++)\n        {\n            var xu = i &lt; x.Length ? x[i] : 0u;\n            var yu = i &lt; y.Length ? y[i] : 0u;\n            z[i] = xu | yu;\n        }\n        return new LongLong(z);\n    }\n    public static LongLong operator ^(LongLong left, LongLong right)\n    {\n        var x = left.ToUIn32Array();\n        var y = right.ToUIn32Array();\n        var z = new uint[Math.Max(x.Length, y.Length)];\n        for (var i = 0; i &lt; z.Length; i++)\n        {\n            var xu = i &lt; x.Length ? x[i] : 0u;\n            var yu = i &lt; y.Length ? y[i] : 0u;\n            z[i] = xu ^ yu;\n        }\n        return new LongLong(z);\n    }\n    public static LongLong operator &amp;(LongLong left, LongLong right)\n    {\n        if (left == 0 || right == 0)\n            return Zero;\n        var x = left.ToUIn32Array();\n        var y = right.ToUIn32Array();\n        var z = new uint[Math.Max(x.Length, y.Length)];\n        for (var i = 0; i &lt; z.Length; i++)\n        {\n            var xu = i &lt; x.Length ? x[i] : 0u;\n            var yu = i &lt; y.Length ? y[i] : 0u;\n            z[i] = xu &amp; yu;\n        }\n        return new LongLong(z);\n    }\n    private class LongLongConverter : 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 LongLong();\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}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Int64 Bit Class 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":[36,5,92],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/179"}],"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=179"}],"version-history":[{"count":2,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/179\/revisions"}],"predecessor-version":[{"id":427,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/179\/revisions\/427"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}