{"id":136,"date":"2020-07-31T12:06:03","date_gmt":"2020-07-31T12:06:03","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=136"},"modified":"2021-06-12T03:08:12","modified_gmt":"2021-06-12T03:08:12","slug":"int128-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/07\/31\/int128-cs\/","title":{"rendered":"(Obsolete) Int128.cs"},"content":{"rendered":"\n<p>Int128 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.Runtime.InteropServices;\nusing System.Text;\n[Serializable]\n[StructLayout(LayoutKind.Sequential)]\n[TypeConverter(typeof(Int128Converter))]\n[DebuggerDisplay(\"{DDisplay}\")]\npublic struct Int128 : IComparable&lt;Int128>, IComparable, IEquatable&lt;Int128>, IConvertible, IFormattable\n{\n    public        ulong _hi;\n    public        ulong _lo;\n    private const ulong HiNeg = 0x8000000000000000;\n    [DebuggerBrowsable(DebuggerBrowsableState.Never)]\n    private string DDisplay => \"0x\" + ToString(\"X1\");\n    public static Int128 Zero;\n    public static Int128 Ten      = new Int128(10);\n    public static Int128 One      = new Int128(1);\n    public static Int128 MaxValue = GetMaxValue();\n    public static Int128 MinValue = GetMinValue();\n    private static Int128 GetMaxValue()\n    {\n        return new Int128(long.MaxValue, ulong.MaxValue);\n    }\n    private static Int128 GetMinValue()\n    {\n        return new Int128(0x8000000000000000, 0);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Either base10 or base 16\n    \/\/\/ &lt;\/summary>\n    public Int128(string value)\n    {\n        TryParse(value, out var result);\n        _hi = result._hi;\n        _lo = result._lo;\n    }\n    public Int128(byte value)\n    {\n        _hi = 0;\n        _lo = value;\n    }\n    public Int128(bool value)\n    {\n        _hi = 0;\n        _lo = (ulong) (value ? 1 : 0);\n    }\n    public Int128(char value)\n    {\n        _hi = 0;\n        _lo = value;\n    }\n    public Int128(decimal value)\n    {\n        if (value &lt; 0)\n        {\n            var n = -new Int128(-value);\n            _hi = n._hi;\n            _lo = n._lo;\n            return;\n        }\n        var bits = decimal.GetBits(value);\n        _hi = (uint) bits[2];\n        _lo = (uint) bits[0] | ((ulong) bits[1] &lt;&lt; 32);\n    }\n    public Int128(double value)\n        : this((decimal) value)\n    {\n    }\n    public Int128(float value)\n        : this((decimal) value)\n    {\n    }\n    public Int128(short value)\n    {\n        if (value &lt; 0)\n        {\n            var n = -new Int128(-(value + 1)) - 1;\n            _hi = n._hi;\n            _lo = n._lo;\n            return;\n        }\n        _hi = 0;\n        _lo = (ulong) value;\n    }\n    public Int128(int value)\n    {\n        if (value &lt; 0)\n        {\n            var n = -new Int128(-(value + 1)) - 1;\n            _hi = n._hi;\n            _lo = n._lo;\n            return;\n        }\n        _hi = 0;\n        _lo = (ulong) value;\n    }\n    public Int128(long value)\n    {\n        if (value &lt; 0)\n        {\n            var n = -new Int128(-(value + 1)) - 1;\n            _hi = n._hi;\n            _lo = n._lo;\n            return;\n        }\n        _hi = 0;\n        _lo = (ulong) value;\n    }\n    public Int128(sbyte value)\n    {\n        if (value &lt; 0)\n        {\n            var n = -new Int128(-(value + 1)) - 1;\n            _hi = n._hi;\n            _lo = n._lo;\n            return;\n        }\n        _hi = 0;\n        _lo = (ulong) value;\n    }\n    public Int128(ushort value)\n    {\n        _hi = 0;\n        _lo = value;\n    }\n    public Int128(uint value)\n    {\n        _hi = 0;\n        _lo = value;\n    }\n    public Int128(ulong value)\n    {\n        _hi = 0;\n        _lo = value;\n    }\n    public Int128(Guid value)\n        : this(value.ToByteArray())\n    {\n    }\n    public Int128(byte[] value)\n    {\n        if (value == null)\n            throw new ArgumentNullException(\"value\");\n        if (value.Length != 16)\n            throw new ArgumentException(null, \"value\");\n        _hi = BitConverter.ToUInt64(value, 8);\n        _lo = BitConverter.ToUInt64(value, 0);\n    }\n    public Int128(ulong hi, ulong lo)\n    {\n        _hi = hi;\n        _lo = lo;\n    }\n    public Int128(int sign, uint[] ints)\n    {\n        if (ints == null)\n            throw new ArgumentNullException(\"ints\");\n        var lo = new byte[8];\n        var hi = new byte[8];\n        if (ints.Length > 0)\n        {\n            Array.Copy(BitConverter.GetBytes(ints[0]), 0, lo, 0, 4);\n            if (ints.Length > 1)\n            {\n                Array.Copy(BitConverter.GetBytes(ints[1]), 0, lo, 4, 4);\n                if (ints.Length > 2)\n                {\n                    Array.Copy(BitConverter.GetBytes(ints[2]), 0, hi, 0, 4);\n                    if (ints.Length > 3)\n                        Array.Copy(BitConverter.GetBytes(ints[3]), 0, hi, 4, 4);\n                }\n            }\n        }\n        _lo = BitConverter.ToUInt64(lo, 0);\n        _hi = BitConverter.ToUInt64(hi, 0);\n        if (sign &lt; 0)\n            _hi |= HiNeg;\n        else\n            _hi &amp;= ~HiNeg;\n    }\n    public int BitWidth\n    {\n        get\n        {\n            Int128 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 int Sign\n    {\n        get\n        {\n            if (_hi == 0 &amp;&amp; _lo == 0)\n                return 0;\n            return (_hi &amp; HiNeg) == 0 ? 1 : -1;\n        }\n    }\n    public override int GetHashCode()\n    {\n        return _hi.GetHashCode() ^ _lo.GetHashCode();\n    }\n    public override bool Equals(object obj)\n    {\n        return base.Equals(obj);\n    }\n    public bool Equals(Int128 obj)\n    {\n        return _hi == obj._hi &amp;&amp; _lo == obj._lo;\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 min;\n                int.TryParse(format.Substring(1).Trim(), out 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 sb = new StringBuilder();\n        var x  = caps ? \"X\" : \"x\";\n        if (min &lt; 0 || min > 16 || _hi != 0)\n        {\n            sb.Append(min > 16 ? _hi.ToString(x + (min - 16)) : _hi.ToString(x));\n            sb.Append(_lo.ToString(x + \"16\"));\n        }\n        else\n        {\n            sb.Append(_lo.ToString(x + min));\n        }\n        return sb.ToString();\n    }\n    private string ToString(NumberFormatInfo info)\n    {\n        if (Sign == 0)\n            return \"0\";\n        var sb      = new StringBuilder();\n        var current = this;\n        current._hi &amp;= ~HiNeg;\n        Int128 r;\n        while (true)\n        {\n            current = DivRem(current, Ten, out r);\n            if (r._lo > 0 || current.Sign != 0 || sb.Length == 0)\n                sb.Insert(0, (char) ('0' + r._lo));\n            if (current.Sign == 0)\n                break;\n        }\n        var s = sb.ToString();\n        if (Sign &lt; 0 &amp;&amp; s != \"0\")\n            return info.NegativeSign + s;\n        return s;\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    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 Int128 Parse(string value)\n    {\n        return Parse(value, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);\n    }\n    public static Int128 Parse(string value, NumberStyles style)\n    {\n        return Parse(value, style, NumberFormatInfo.CurrentInfo);\n    }\n    public static Int128 Parse(string value, IFormatProvider provider)\n    {\n        return Parse(value, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));\n    }\n    public static Int128 Parse(string value, NumberStyles style, IFormatProvider provider)\n    {\n        Int128 result;\n        if (!TryParse(value, style, provider, out result))\n            throw new ArgumentException(null, \"value\");\n        return result;\n    }\n    public static bool TryParse(string value, out Int128 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 Int128 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 Int128 result)\n    {\n        if (value.Length > 32)\n            throw new OverflowException();\n        result = Zero;\n        var hi  = false;\n        var pos = 0;\n        for (var i = value.Length - 1; i >= 0; i--)\n        {\n            var   ch = value[i];\n            ulong b;\n            if (ch >= '0' &amp;&amp; ch &lt;= '9')\n                b = (ulong) (ch - '0');\n            else if (ch >= 'A' &amp;&amp; ch &lt;= 'F')\n                b = (ulong) (ch - 'A' + 10);\n            else if (ch >= 'a' &amp;&amp; ch &lt;= 'f')\n                b = (ulong) (ch - 'a' + 10);\n            else\n                return false;\n            if (hi)\n            {\n                result._hi |= b &lt;&lt; pos;\n                pos        += 4;\n            }\n            else\n            {\n                result._lo |= b &lt;&lt; pos;\n                pos        += 4;\n                if (pos == 64)\n                {\n                    pos = 0;\n                    hi  = true;\n                }\n            }\n        }\n        return true;\n    }\n    private static bool TryParseNum(string value, out Int128 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        if (_hi != 0)\n            throw new OverflowException();\n        return Convert.ToUInt16(_lo);\n    }\n    uint IConvertible.ToUInt32(IFormatProvider provider)\n    {\n        if (_hi != 0)\n            throw new OverflowException();\n        return Convert.ToUInt32(_lo);\n    }\n    ulong IConvertible.ToUInt64(IFormatProvider provider)\n    {\n        if (_hi != 0)\n            throw new OverflowException();\n        return _lo;\n    }\n    int IComparable.CompareTo(object obj)\n    {\n        return Compare(this, obj);\n    }\n    public static int Compare(Int128 left, object right)\n    {\n        if (right is Int128)\n            return Compare(left, (Int128) right);\n        if (right is bool)\n            return Compare(left, new Int128((bool) right));\n        if (right is byte)\n            return Compare(left, new Int128((byte) right));\n        if (right is char)\n            return Compare(left, new Int128((char) right));\n        if (right is decimal)\n            return Compare(left, new Int128((decimal) right));\n        if (right is double)\n            return Compare(left, new Int128((double) right));\n        if (right is short)\n            return Compare(left, new Int128((short) right));\n        if (right is int)\n            return Compare(left, new Int128((int) right));\n        if (right is long)\n            return Compare(left, new Int128((long) right));\n        if (right is sbyte)\n            return Compare(left, new Int128((sbyte) right));\n        if (right is float)\n            return Compare(left, new Int128((float) right));\n        if (right is ushort)\n            return Compare(left, new Int128((ushort) right));\n        if (right is uint)\n            return Compare(left, new Int128((uint) right));\n        if (right is ulong)\n            return Compare(left, new Int128((ulong) right));\n        var bytes = right as byte[];\n        if (bytes != null &amp;&amp; bytes.Length != 16)\n            return Compare(left, new Int128(bytes));\n        if (right is Guid)\n            return Compare(left, new Int128((Guid) right));\n        throw new ArgumentException();\n    }\n    public byte[] ToByteArray()\n    {\n        var bytes = new byte[16];\n        Buffer.BlockCopy(BitConverter.GetBytes(_lo), 0, bytes, 0, 8);\n        Buffer.BlockCopy(BitConverter.GetBytes(_hi), 0, bytes, 8, 8);\n        return bytes;\n    }\n    public static int Compare(Int128 left, Int128 right)\n    {\n        if (left.Sign &lt; 0)\n        {\n            if (right.Sign >= 0)\n                return -1;\n            var xhi = left._hi  &amp; ~HiNeg;\n            var yhi = right._hi &amp; ~HiNeg;\n            if (xhi != yhi)\n                return -xhi.CompareTo(yhi);\n            return -left._lo.CompareTo(right._lo);\n        }\n        if (right.Sign &lt; 0)\n            return 1;\n        if (left._hi != right._hi)\n            return left._hi.CompareTo(right._hi);\n        return left._lo.CompareTo(right._lo);\n    }\n    public int CompareTo(Int128 value)\n    {\n        return Compare(this, value);\n    }\n    public static implicit operator Int128(bool value)\n    {\n        return new Int128(value);\n    }\n    public static implicit operator Int128(byte value)\n    {\n        return new Int128(value);\n    }\n    public static implicit operator Int128(char value)\n    {\n        return new Int128(value);\n    }\n    public static explicit operator Int128(decimal value)\n    {\n        return new Int128(value);\n    }\n    public static explicit operator Int128(double value)\n    {\n        return new Int128(value);\n    }\n    public static implicit operator Int128(short value)\n    {\n        return new Int128(value);\n    }\n    public static implicit operator Int128(int value)\n    {\n        return new Int128(value);\n    }\n    public static implicit operator Int128(long value)\n    {\n        return new Int128(value);\n    }\n    public static implicit operator Int128(sbyte value)\n    {\n        return new Int128(value);\n    }\n    public static explicit operator Int128(float value)\n    {\n        return new Int128(value);\n    }\n    public static implicit operator Int128(ushort value)\n    {\n        return new Int128(value);\n    }\n    public static implicit operator Int128(uint value)\n    {\n        return new Int128(value);\n    }\n    public static implicit operator Int128(ulong value)\n    {\n        return new Int128(value);\n    }\n    public static explicit operator bool(Int128 value)\n    {\n        return value.Sign != 0;\n    }\n    public static explicit operator byte(Int128 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        if (value.Sign &lt; 0 || value._lo > 0xFF)\n            throw new OverflowException();\n        return (byte) value._lo;\n    }\n    public static explicit operator char(Int128 value)\n    {\n        if (value.Sign == 0)\n            return (char) 0;\n        if (value.Sign &lt; 0 || value._lo > 0xFFFF)\n            throw new OverflowException();\n        return (char) (ushort) value._lo;\n    }\n    public static explicit operator decimal(Int128 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        return new decimal((int) (value._lo &amp; 0xFFFFFFFF), (int) (value._lo >> 32), (int) (value._hi &amp; 0xFFFFFFFF),\n            value.Sign &lt; 0, 0);\n    }\n    public static explicit operator double(Int128 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        double d;\n        var    nfi = CultureInfo.InvariantCulture.NumberFormat;\n        if (!double.TryParse(value.ToString(nfi), NumberStyles.Number, nfi, out d))\n            throw new OverflowException();\n        return d;\n    }\n    public static explicit operator float(Int128 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        float f;\n        var   nfi = CultureInfo.InvariantCulture.NumberFormat;\n        if (!float.TryParse(value.ToString(nfi), NumberStyles.Number, nfi, out f))\n            throw new OverflowException();\n        return f;\n    }\n    public static explicit operator short(Int128 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        if (value._lo > 0x8000)\n            throw new OverflowException();\n        if (value._lo == 0x8000 &amp;&amp; value.Sign > 0)\n            throw new OverflowException();\n        return (short) ((int) value._lo * value.Sign);\n    }\n    public static explicit operator int(Int128 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        if (value._lo > 0x80000000)\n            throw new OverflowException();\n        if (value._lo == 0x80000000 &amp;&amp; value.Sign > 0)\n            throw new OverflowException();\n        return (int) value._lo * value.Sign;\n    }\n    public static explicit operator long(Int128 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        if (value._lo > long.MaxValue)\n            throw new OverflowException();\n        return (long) value._lo * value.Sign;\n    }\n    public static explicit operator uint(Int128 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        if (value.Sign &lt; 0 || value._lo > uint.MaxValue)\n            throw new OverflowException();\n        return (uint) value._lo;\n    }\n    public static explicit operator ushort(Int128 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        if (value.Sign &lt; 0 || value._lo > ushort.MaxValue)\n            throw new OverflowException();\n        return (ushort) value._lo;\n    }\n    public static explicit operator ulong(Int128 value)\n    {\n        if (value.Sign &lt; 0 || value._hi != 0)\n            throw new OverflowException();\n        return value._lo;\n    }\n    public static bool operator >(Int128 left, Int128 right)\n    {\n        return Compare(left, right) > 0;\n    }\n    public static bool operator &lt;(Int128 left, Int128 right)\n    {\n        return Compare(left, right) &lt; 0;\n    }\n    public static bool operator >=(Int128 left, Int128 right)\n    {\n        return Compare(left, right) >= 0;\n    }\n    public static bool operator &lt;=(Int128 left, Int128 right)\n    {\n        return Compare(left, right) &lt;= 0;\n    }\n    public static bool operator !=(Int128 left, Int128 right)\n    {\n        return Compare(left, right) != 0;\n    }\n    public static bool operator ==(Int128 left, Int128 right)\n    {\n        return Compare(left, right) == 0;\n    }\n    public static Int128 operator +(Int128 value)\n    {\n        return value;\n    }\n    public static Int128 operator -(Int128 value)\n    {\n        return Negate(value);\n    }\n    public static Int128 operator ~(Int128 value)\n    {\n        return -(value + One);\n    }\n    public static Int128 operator ++(Int128 value)\n    {\n        return value + 1;\n    }\n    public static Int128 operator --(Int128 value)\n    {\n        return value - 1;\n    }\n    public static Int128 Negate(Int128 value)\n    {\n        return new Int128(~value._hi, ~value._lo) + 1;\n    }\n    public Int128 ToAbs()\n    {\n        return Abs(this);\n    }\n    public static Int128 Abs(Int128 value)\n    {\n        if (value.Sign &lt; 0)\n            return -value;\n        return value;\n    }\n    public static Int128 operator +(Int128 left, Int128 right)\n    {\n        var add = left;\n        add._hi += right._hi;\n        add._lo += right._lo;\n        if (add._lo &lt; left._lo)\n            add._hi++;\n        return add;\n    }\n    public static Int128 operator -(Int128 left, Int128 right)\n    {\n        return left + -right;\n    }\n    public static Int128 Add(Int128 left, Int128 right)\n    {\n        return left + right;\n    }\n    public static Int128 Subtract(Int128 left, Int128 right)\n    {\n        return left - right;\n    }\n    public static Int128 Divide(Int128 dividend, Int128 divisor)\n    {\n        Int128 integer;\n        return DivRem(dividend, divisor, out integer);\n    }\n    public static Int128 DivRem(Int128 dividend, Int128 divisor, out Int128 remainder)\n    {\n        if (divisor == 0)\n            throw new DivideByZeroException();\n        uint[] quotient;\n        uint[] rem;\n        DivRem(dividend.ToUIn32Array(), divisor.ToUIn32Array(), out quotient, out rem);\n        remainder = new Int128(1, rem);\n        return new Int128(dividend.Sign * divisor.Sign, 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                long di = 0;\n                long 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                      = (long) 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 unormalized = 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                unormalized[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                unormalized[j] = normalized[j];\n        }\n        return unormalized;\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 Int128 Remainder(Int128 dividend, Int128 divisor)\n    {\n        Int128 remainder;\n        DivRem(dividend, divisor, out remainder);\n        return remainder;\n    }\n    public static Int128 operator %(Int128 dividend, Int128 divisor)\n    {\n        return Remainder(dividend, divisor);\n    }\n    public static Int128 operator \/(Int128 dividend, Int128 divisor)\n    {\n        return Divide(dividend, divisor);\n    }\n    public ulong[] ToUIn64Array()\n    {\n        return new[] {_hi, _lo};\n    }\n    public uint[] ToUIn32Array()\n    {\n        var ints = new uint[4];\n        var lob  = BitConverter.GetBytes(_lo);\n        var hib  = BitConverter.GetBytes(_hi);\n        Buffer.BlockCopy(lob, 0, ints, 0,  4);\n        Buffer.BlockCopy(lob, 4, ints, 4,  4);\n        Buffer.BlockCopy(hib, 0, ints, 8,  4);\n        Buffer.BlockCopy(hib, 4, ints, 12, 4);\n        return ints;\n    }\n    public static Int128 Multiply(Int128 left, Int128 right)\n    {\n        var xInts   = left.ToUIn32Array();\n        var yInts   = right.ToUIn32Array();\n        var mulInts = new uint[8];\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 Int128(left.Sign * right.Sign, mulInts);\n    }\n    public static Int128 operator *(Int128 left, Int128 right)\n    {\n        return Multiply(left, right);\n    }\n    public static Int128 operator >>(Int128 value, int shift)\n    {\n        if (shift == 0)\n            return value;\n        if (shift &lt; 0)\n            return value &lt;&lt; -shift;\n        shift = shift % 128;\n        var shifted = new Int128();\n        if (shift > 63)\n        {\n            shifted._lo = value._hi >> (shift - 64);\n            shifted._hi = 0;\n        }\n        else\n        {\n            shifted._hi = value._hi >> shift;\n            shifted._lo = (value._hi &lt;&lt; (64 - shift)) | (value._lo >> shift);\n        }\n        return shifted;\n    }\n    public static Int128 operator &lt;&lt;(Int128 value, int shift)\n    {\n        if (shift == 0)\n            return value;\n        if (shift &lt; 0)\n            return value >> -shift;\n        shift = shift % 128;\n        var shifted = new Int128();\n        if (shift > 63)\n        {\n            shifted._hi = value._lo &lt;&lt; (shift - 64);\n            shifted._lo = 0;\n        }\n        else\n        {\n            var ul = value._lo            >> (64 - shift);\n            shifted._hi = ul | (value._hi &lt;&lt; shift);\n            shifted._lo = value._lo &lt;&lt; shift;\n        }\n        return shifted;\n    }\n    public static Int128 operator |(Int128 left, Int128 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        var xExtend = left.Sign  &lt; 0 ? uint.MaxValue : 0;\n        var yExtend = right.Sign &lt; 0 ? uint.MaxValue : 0;\n        for (var i = 0; i &lt; z.Length; i++)\n        {\n            var xu = i &lt; x.Length ? x[i] : xExtend;\n            var yu = i &lt; y.Length ? y[i] : yExtend;\n            z[i] = xu | yu;\n        }\n        return new Int128(left.Sign * right.Sign, z);\n    }\n    public static Int128 operator ^(Int128 left, Int128 right)\n    {\n        var x       = left.ToUIn32Array();\n        var y       = right.ToUIn32Array();\n        var z       = new uint[Math.Max(x.Length, y.Length)];\n        var xExtend = left.Sign  &lt; 0 ? uint.MaxValue : 0;\n        var yExtend = right.Sign &lt; 0 ? uint.MaxValue : 0;\n        for (var i = 0; i &lt; z.Length; i++)\n        {\n            var xu = i &lt; x.Length ? x[i] : xExtend;\n            var yu = i &lt; y.Length ? y[i] : yExtend;\n            z[i] = xu ^ yu;\n        }\n        return new Int128(left.Sign * right.Sign, z);\n    }\n    public static Int128 operator &amp;(Int128 left, Int128 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        var xExtend = left.Sign  &lt; 0 ? uint.MaxValue : 0;\n        var yExtend = right.Sign &lt; 0 ? uint.MaxValue : 0;\n        for (var i = 0; i &lt; z.Length; i++)\n        {\n            var xu = i &lt; x.Length ? x[i] : xExtend;\n            var yu = i &lt; y.Length ? y[i] : yExtend;\n            z[i] = xu &amp; yu;\n        }\n        return new Int128(left.Sign * right.Sign, z);\n    }\n    public class Int128Converter : TypeConverter\n    {\n        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)\n        {\n            if (sourceType == typeof(string))\n                return true;\n            return base.CanConvertFrom(context, sourceType);\n        }\n        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)\n        {\n            if (value != null)\n            {\n                Int128 i;\n                if (TryParse($\"{value}\", out i))\n                    return i;\n            }\n            return new Int128();\n        }\n        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)\n        {\n            if (destinationType == typeof(string))\n                return true;\n            return base.CanConvertTo(context, destinationType);\n        }\n        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value,\n            Type                                                destinationType)\n        {\n            if (destinationType == typeof(string))\n                return $\"{value}\";\n            return base.ConvertTo(context, culture, value, destinationType);\n        }\n        \n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Int128 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":[5,66],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/136"}],"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=136"}],"version-history":[{"count":3,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/136\/revisions"}],"predecessor-version":[{"id":445,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/136\/revisions\/445"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}