{"id":175,"date":"2020-08-11T02:57:22","date_gmt":"2020-08-11T02:57:22","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=175"},"modified":"2021-06-12T03:07:53","modified_gmt":"2021-06-12T03:07:53","slug":"int256-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/08\/11\/int256-cs\/","title":{"rendered":"(Obsolete) Int256.cs"},"content":{"rendered":"\n<p>Int256 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\n[Serializable]\n[StructLayout(LayoutKind.Sequential, Pack = 1)]\n[TypeConverter(typeof(Int256Converter))]\n[DebuggerDisplay(\"{DDisplay}\")]\npublic struct Int256 : IComparable&lt;Int256>, IComparable, IEquatable&lt;Int256>, IConvertible, IFormattable\n{\n    public ulong Bytes24To32;\n    public ulong Bytes16To24;\n    public ulong Bytes8To16;\n    public ulong Bytes0To8;\n    private const ulong HiNeg = 0x8000000000000000;\n\n    [DebuggerBrowsable(DebuggerBrowsableState.Never)]\n    private string DDisplay => ToString();\n\n    public static Int256 Zero = new Int256(0);\n    public static Int256 Ten = new Int256(10);\n    public static Int256 One = new Int256(1);\n    public static Int256 MaxValue = GetMaxValue();\n    public static Int256 MinValue = GetMinValue();\n\n    private static Int256 GetMaxValue()\n    {\n        return new Int256(long.MaxValue, ulong.MaxValue, ulong.MaxValue, ulong.MaxValue);\n    }\n\n    private static Int256 GetMinValue()\n    {\n        return -GetMaxValue();\n    }\n    public Int256(string value)\n    {\n        TryParse(value, out var result);\n        Bytes24To32 = result.Bytes24To32;\n        Bytes16To24 = result.Bytes16To24;\n        Bytes8To16 = result.Bytes8To16;\n        Bytes0To8 = result.Bytes0To8;\n    }\n    public Int256(byte value)\n    {\n        Bytes24To32 = 0;\n        Bytes16To24 = 0;\n        Bytes8To16 = 0;\n        Bytes0To8 = value;\n    }\n\n    public Int256(bool value)\n    {\n        Bytes24To32 = 0;\n        Bytes16To24 = 0;\n        Bytes8To16 = 0;\n        Bytes0To8 = (ulong) (value ? 1 : 0);\n    }\n\n    public Int256(char value)\n    {\n        Bytes24To32 = 0;\n        Bytes16To24 = 0;\n        Bytes8To16 = 0;\n        Bytes0To8 = value;\n    }\n\n    public Int256(decimal value)\n    {\n        if (value &lt; 0)\n        {\n            var n = -new Int256(-value);\n            Bytes24To32 = n.Bytes24To32;\n            Bytes16To24 = n.Bytes16To24;\n            Bytes8To16 = n.Bytes8To16;\n            Bytes0To8 = n.Bytes0To8;\n            return;\n        }\n\n        var bits = decimal.GetBits(value);\n        Bytes24To32 = 0;\n        Bytes16To24 = (uint) bits[2];\n        Bytes8To16 = (uint) bits[1];\n        Bytes0To8 = (uint) bits[0];\n        if (value &lt; 0)\n        {\n            Bytes24To32 = ~Bytes24To32;\n            Bytes16To24 = ~Bytes16To24;\n            Bytes8To16 = ~Bytes8To16;\n            Bytes0To8 = ~Bytes0To8;\n        }\n    }\n\n    public Int256(double value)\n        : this((decimal) value)\n    {\n    }\n\n    public Int256(float value)\n        : this((decimal) value)\n    {\n    }\n\n    public Int256(short value)\n    {\n        if (value &lt; 0)\n        {\n            var n = -new Int256(-(value + 1)) - 1;\n            Bytes24To32 = n.Bytes24To32;\n            Bytes16To24 = n.Bytes16To24;\n            Bytes8To16 = n.Bytes8To16;\n            Bytes0To8 = n.Bytes0To8;\n            return;\n        }\n\n        Bytes24To32 = 0;\n        Bytes16To24 = 0;\n        Bytes8To16 = 0;\n        Bytes0To8 = (ulong) value;\n    }\n\n    public Int256(int value)\n    {\n        if (value &lt; 0)\n        {\n            var n = -new Int256(-(value + 1)) - 1;\n            Bytes24To32 = n.Bytes24To32;\n            Bytes16To24 = n.Bytes16To24;\n            Bytes8To16 = n.Bytes8To16;\n            Bytes0To8 = n.Bytes0To8;\n            return;\n        }\n\n        Bytes24To32 = 0;\n        Bytes16To24 = 0;\n        Bytes8To16 = 0;\n        Bytes0To8 = (ulong) value;\n    }\n\n    public Int256(long value)\n    {\n        if (value &lt; 0)\n        {\n            var n = -new Int256(-(value + 1)) - 1;\n            Bytes24To32 = n.Bytes24To32;\n            Bytes16To24 = n.Bytes16To24;\n            Bytes8To16 = n.Bytes8To16;\n            Bytes0To8 = n.Bytes0To8;\n            return;\n        }\n\n        Bytes24To32 = 0;\n        Bytes16To24 = 0;\n        Bytes8To16 = 0;\n        Bytes0To8 = (ulong) value;\n    }\n\n    public Int256(sbyte value)\n    {\n        if (value &lt; 0)\n        {\n            var n = -new Int256(-(value + 1)) - 1;\n            Bytes24To32 = n.Bytes24To32;\n            Bytes16To24 = n.Bytes16To24;\n            Bytes8To16 = n.Bytes8To16;\n            Bytes0To8 = n.Bytes0To8;\n            return;\n        }\n\n        Bytes24To32 = 0;\n        Bytes16To24 = 0;\n        Bytes8To16 = 0;\n        Bytes0To8 = (ulong) value;\n    }\n\n    public Int256(ushort value)\n    {\n        Bytes24To32 = 0;\n        Bytes16To24 = 0;\n        Bytes8To16 = 0;\n        Bytes0To8 = value;\n    }\n\n    public Int256(uint value)\n    {\n        Bytes24To32 = 0;\n        Bytes16To24 = 0;\n        Bytes8To16 = 0;\n        Bytes0To8 = value;\n    }\n\n    public Int256(ulong value)\n    {\n        Bytes24To32 = 0;\n        Bytes16To24 = 0;\n        Bytes8To16 = 0;\n        Bytes0To8 = value;\n    }\n\n    public Int256(Guid value)\n        : this(value.ToByteArray())\n    {\n    }\n\n    public Int256(byte[] value)\n    {\n        if (value == null)\n            throw new Exception(\"Value cannot be null.\");\n        if (value.Length != 32)\n            throw new Exception(\"Values length must be 32 bytes.\");\n        Bytes24To32 = BitConverter.ToUInt64(value, 24);\n        Bytes16To24 = BitConverter.ToUInt64(value, 16);\n        Bytes8To16 = BitConverter.ToUInt64(value, 8);\n        Bytes0To8 = BitConverter.ToUInt64(value, 0);\n    }\n\n    public Int256(ulong msbh, ulong msbl, ulong lsbh, ulong lsbl)\n    {\n        Bytes24To32 = msbh;\n        Bytes16To24 = msbl;\n        Bytes8To16 = lsbh;\n        Bytes0To8 = lsbl;\n    }\n\n    public Int256(int sign, uint[] array)\n    {\n        if (array == null)\n            throw new Exception(\"Array cannot be null.\");\n        var msbh = new byte[8];\n        var msbl = new byte[8];\n        var lsbh = new byte[8];\n        var lsbl = new byte[8];\n        if (array.Length > 0)\n        {\n            Array.Copy(BitConverter.GetBytes(array[0]), 0, lsbl, 0, 4);\n            if (array.Length > 1)\n            {\n                Array.Copy(BitConverter.GetBytes(array[1]), 0, lsbl, 4, 4);\n                if (array.Length > 2)\n                {\n                    Array.Copy(BitConverter.GetBytes(array[2]), 0, lsbh, 0, 4);\n                    if (array.Length > 3)\n                    {\n                        Array.Copy(BitConverter.GetBytes(array[3]), 0, lsbh, 4, 4);\n                        if (array.Length > 4)\n                        {\n                            Array.Copy(BitConverter.GetBytes(array[4]), 0, msbl, 0, 4);\n                            if (array.Length > 5)\n                            {\n                                Array.Copy(BitConverter.GetBytes(array[5]), 0, msbl, 4, 4);\n                                if (array.Length > 6)\n                                {\n                                    Array.Copy(BitConverter.GetBytes(array[6]), 0, msbh, 0, 4);\n                                    if (array.Length > 7)\n                                        Array.Copy(BitConverter.GetBytes(array[7]), 0, msbh, 4, 4);\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n\n        Bytes24To32 = BitConverter.ToUInt64(msbh, 0);\n        Bytes16To24 = BitConverter.ToUInt64(msbl, 0);\n        Bytes8To16 = BitConverter.ToUInt64(lsbh, 0);\n        Bytes0To8 = BitConverter.ToUInt64(lsbl, 0);\n        if (sign &lt; 0)\n            Bytes24To32 |= HiNeg;\n        else\n            Bytes24To32 &amp;= ~HiNeg;\n    }\n\n    public ulong MSBH => Bytes24To32;\n    public ulong MSBL => Bytes16To24;\n    public ulong LSBH => Bytes8To16;\n    public ulong LSBL => Bytes0To8;\n\n    public int BitWidth\n    {\n        get\n        {\n            Int256 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\n    public int Sign\n    {\n        get\n        {\n            if (Bytes24To32 == 0 &amp;&amp; Bytes16To24 == 0 &amp;&amp; Bytes8To16 == 0 &amp;&amp; Bytes0To8 == 0)\n                return 0;\n            return (Bytes24To32 &amp; HiNeg) == 0 ? 1 : -1;\n        }\n    }\n\n    public override int GetHashCode()\n    {\n        return MSBH.GetHashCode() ^ MSBL.GetHashCode() ^ LSBH.GetHashCode() ^ LSBL.GetHashCode();\n    }\n\n    public override bool Equals(object obj)\n    {\n        return base.Equals(obj);\n    }\n\n    public bool Equals(Int256 obj)\n    {\n        return Bytes24To32 == obj.Bytes24To32 &amp;&amp; Bytes16To24 == obj.Bytes16To24 &amp;&amp; Bytes8To16 == obj.Bytes8To16 &amp;&amp;\n               Bytes0To8 == obj.Bytes0To8;\n    }\n\n    public override string ToString()\n    {\n        return ToString(null, null);\n    }\n\n    public string ToString(string format)\n    {\n        return ToString(format, null);\n    }\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\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\n        return ToString((NumberFormatInfo) formatProvider.GetFormat(typeof(NumberFormatInfo)));\n    }\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\n        return sb.ToString();\n    }\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.Bytes24To32 &amp;= ~HiNeg;\n        while (true)\n        {\n            current = DivRem(current, Ten, out var r);\n            if (r.Bytes0To8 > 0 || current.Sign != 0 || sb.Length == 0)\n                sb.Insert(0, (char) ('0' + r.Bytes0To8));\n            if (current.Sign == 0)\n                break;\n        }\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\n    TypeCode IConvertible.GetTypeCode()\n    {\n        return TypeCode.Object;\n    }\n\n    bool IConvertible.ToBoolean(IFormatProvider provider)\n    {\n        return (bool) this;\n    }\n\n    byte IConvertible.ToByte(IFormatProvider provider)\n    {\n        return (byte) this;\n    }\n\n    char IConvertible.ToChar(IFormatProvider provider)\n    {\n        return (char) this;\n    }\n\n    DateTime IConvertible.ToDateTime(IFormatProvider provider)\n    {\n        throw new InvalidCastException();\n    }\n\n    decimal IConvertible.ToDecimal(IFormatProvider provider)\n    {\n        return (decimal) this;\n    }\n\n    double IConvertible.ToDouble(IFormatProvider provider)\n    {\n        return (double) this;\n    }\n\n    short IConvertible.ToInt16(IFormatProvider provider)\n    {\n        return (short) this;\n    }\n\n    int IConvertible.ToInt32(IFormatProvider provider)\n    {\n        return (int) this;\n    }\n\n    long IConvertible.ToInt64(IFormatProvider provider)\n    {\n        return (int) this;\n    }\n\n    sbyte IConvertible.ToSByte(IFormatProvider provider)\n    {\n        return (sbyte) this;\n    }\n\n    float IConvertible.ToSingle(IFormatProvider provider)\n    {\n        return (float) this;\n    }\n\n    string IConvertible.ToString(IFormatProvider provider)\n    {\n        return ToString(null, provider);\n    }\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\n        if (conversionType == typeof(byte))\n        {\n            value = (byte) this;\n            return true;\n        }\n\n        if (conversionType == typeof(char))\n        {\n            value = (char) this;\n            return true;\n        }\n\n        if (conversionType == typeof(decimal))\n        {\n            value = (decimal) this;\n            return true;\n        }\n\n        if (conversionType == typeof(double))\n        {\n            value = (double) this;\n            return true;\n        }\n\n        if (conversionType == typeof(short))\n        {\n            value = (short) this;\n            return true;\n        }\n\n        if (conversionType == typeof(int))\n        {\n            value = (int) this;\n            return true;\n        }\n\n        if (conversionType == typeof(long))\n        {\n            value = (long) this;\n            return true;\n        }\n\n        if (conversionType == typeof(sbyte))\n        {\n            value = (sbyte) this;\n            return true;\n        }\n\n        if (conversionType == typeof(float))\n        {\n            value = (float) this;\n            return true;\n        }\n\n        if (conversionType == typeof(string))\n        {\n            value = ToString(null, provider);\n            return true;\n        }\n\n        if (conversionType == typeof(ushort))\n        {\n            value = (ushort) this;\n            return true;\n        }\n\n        if (conversionType == typeof(uint))\n        {\n            value = (uint) this;\n            return true;\n        }\n\n        if (conversionType == typeof(ulong))\n        {\n            value = (ulong) this;\n            return true;\n        }\n\n        if (conversionType == typeof(byte[]))\n        {\n            value = ToByteArray();\n            return true;\n        }\n\n        if (conversionType == typeof(Guid))\n        {\n            value = new Guid(ToByteArray());\n            return true;\n        }\n\n        value = null;\n        return false;\n    }\n\n    public static Int256 Parse(string value)\n    {\n        return Parse(value, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);\n    }\n\n    public static Int256 Parse(string value, NumberStyles style)\n    {\n        return Parse(value, style, NumberFormatInfo.CurrentInfo);\n    }\n\n    public static Int256 Parse(string value, IFormatProvider provider)\n    {\n        return Parse(value, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));\n    }\n\n    public static Int256 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\n    public static bool TryParse(string value, out Int256 result)\n    {\n        return TryParse(value, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);\n    }\n\n    public static bool TryParse(string value, NumberStyles style, IFormatProvider provider, out Int256 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\n        if ((style &amp; NumberStyles.AllowHexSpecifier) == NumberStyles.AllowHexSpecifier)\n            return TryParseHex(value, out result);\n        return TryParseNum(value, out result);\n    }\n\n    private static bool TryParseHex(string value, out Int256 result)\n    {\n        if (value.Length > 64)\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.Bytes0To8 |= bch &lt;&lt; pos;\n            else if (pos &lt; 128)\n                result.Bytes8To16 |= bch &lt;&lt; pos;\n            else if (pos &lt; 192)\n                result.Bytes16To24 |= bch &lt;&lt; pos;\n            else if (pos &lt; 256)\n                result.Bytes24To32 |= bch &lt;&lt; pos;\n            pos += 4;\n        }\n\n        return true;\n    }\n\n    private static bool TryParseNum(string value, out Int256 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\n        return true;\n    }\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\n    ushort IConvertible.ToUInt16(IFormatProvider provider)\n    {\n        if (Bytes8To16 != 0)\n            throw new OverflowException();\n        return Convert.ToUInt16(Bytes0To8);\n    }\n\n    uint IConvertible.ToUInt32(IFormatProvider provider)\n    {\n        if (Bytes8To16 != 0)\n            throw new OverflowException();\n        return Convert.ToUInt32(Bytes0To8);\n    }\n\n    ulong IConvertible.ToUInt64(IFormatProvider provider)\n    {\n        if (Bytes8To16 != 0)\n            throw new OverflowException();\n        return Bytes0To8;\n    }\n\n    int IComparable.CompareTo(object obj)\n    {\n        return Compare(this, obj);\n    }\n\n    public static int Compare(Int256 left, object right)\n    {\n        if (right is Int256)\n            return Compare(left, (Int256) right);\n        if (right is bool)\n            return Compare(left, new Int256((bool) right));\n        if (right is byte)\n            return Compare(left, new Int256((byte) right));\n        if (right is char)\n            return Compare(left, new Int256((char) right));\n        if (right is decimal)\n            return Compare(left, new Int256((decimal) right));\n        if (right is double)\n            return Compare(left, new Int256((double) right));\n        if (right is short)\n            return Compare(left, new Int256((short) right));\n        if (right is int)\n            return Compare(left, new Int256((int) right));\n        if (right is long)\n            return Compare(left, new Int256((long) right));\n        if (right is sbyte)\n            return Compare(left, new Int256((sbyte) right));\n        if (right is float)\n            return Compare(left, new Int256((float) right));\n        if (right is ushort)\n            return Compare(left, new Int256((ushort) right));\n        if (right is uint)\n            return Compare(left, new Int256((uint) right));\n        if (right is ulong)\n            return Compare(left, new Int256((ulong) right));\n        var bytes = right as byte[];\n        if (bytes != null &amp;&amp; bytes.Length != 32)\n            return Compare(left, new Int256(bytes));\n        if (right is Guid)\n            return Compare(left, new Int256((Guid) right));\n        throw new ArgumentException();\n    }\n\n    public static int Compare(Int256 left, Int256 right)\n    {\n        var leftSign = left.Sign;\n        var rightSign = right.Sign;\n        if (leftSign == 0 &amp;&amp; rightSign == 0)\n            return 0;\n        if (leftSign >= 0 &amp;&amp; rightSign &lt; 0)\n            return 1;\n        if (leftSign &lt; 0 &amp;&amp; rightSign >= 0)\n            return -1;\n        if (left.Bytes24To32 != right.Bytes24To32)\n            return left.Bytes24To32.CompareTo(right.Bytes24To32);\n        if (left.Bytes16To24 != right.Bytes16To24)\n            return left.Bytes16To24.CompareTo(right.Bytes16To24);\n        if (left.Bytes8To16 != right.Bytes8To16)\n            return left.Bytes8To16.CompareTo(right.Bytes8To16);\n        return left.Bytes0To8.CompareTo(right.Bytes0To8);\n    }\n\n    public int CompareTo(Int256 value)\n    {\n        return Compare(this, value);\n    }\n\n    public static implicit operator Int256(bool value)\n    {\n        return new Int256(value);\n    }\n\n    public static implicit operator Int256(byte value)\n    {\n        return new Int256(value);\n    }\n\n    public static implicit operator Int256(char value)\n    {\n        return new Int256(value);\n    }\n\n    public static explicit operator Int256(decimal value)\n    {\n        return new Int256(value);\n    }\n\n    public static explicit operator Int256(double value)\n    {\n        return new Int256(value);\n    }\n\n    public static implicit operator Int256(short value)\n    {\n        return new Int256(value);\n    }\n\n    public static implicit operator Int256(int value)\n    {\n        return new Int256(value);\n    }\n\n    public static implicit operator Int256(long value)\n    {\n        return new Int256(value);\n    }\n\n    public static implicit operator Int256(sbyte value)\n    {\n        return new Int256(value);\n    }\n\n    public static explicit operator Int256(float value)\n    {\n        return new Int256(value);\n    }\n\n    public static implicit operator Int256(ushort value)\n    {\n        return new Int256(value);\n    }\n\n    public static implicit operator Int256(uint value)\n    {\n        return new Int256(value);\n    }\n\n    public static implicit operator Int256(ulong value)\n    {\n        return new Int256(value);\n    }\n\n    public static explicit operator bool(Int256 value)\n    {\n        return value.Sign != 0;\n    }\n\n    public static explicit operator byte(Int256 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        if (value.Sign &lt; 0 || value.Bytes0To8 > 0xFF)\n            throw new OverflowException();\n        return (byte) value.Bytes0To8;\n    }\n\n    public static explicit operator char(Int256 value)\n    {\n        if (value.Sign == 0)\n            return (char) 0;\n        if (value.Sign &lt; 0 || value.Bytes0To8 > 0xFFFF)\n            throw new OverflowException();\n        return (char) (ushort) value.Bytes0To8;\n    }\n\n    public static explicit operator decimal(Int256 value)\n    {\n        return value.Sign == 0\n            ? 0\n            : new decimal((int) (value.Bytes0To8 &amp; 0xFFFFFFFF), (int) (value.Bytes8To16 &amp; 0xFFFFFFFF),\n                (int) (value.Bytes16To24 &amp; 0xFFFFFFFF), value.Sign &lt; 0, 0);\n    }\n\n    public static explicit operator double(Int256 value)\n    {\n        if (value.Sign == 0)\n            return 0;\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\n    public static explicit operator float(Int256 value)\n    {\n        if (value.Sign == 0)\n            return 0;\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\n    public static explicit operator short(Int256 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        if (value.Bytes0To8 > 0x8000)\n            throw new OverflowException();\n        if (value.Bytes0To8 == 0x8000 &amp;&amp; value.Sign > 0)\n            throw new OverflowException();\n        return (short) ((int) value.Bytes0To8 * value.Sign);\n    }\n\n    public static explicit operator int(Int256 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        if (value.Bytes0To8 > 0x80000000)\n            throw new OverflowException();\n        if (value.Bytes0To8 == 0x80000000 &amp;&amp; value.Sign > 0)\n            throw new OverflowException();\n        return (int) value.Bytes0To8 * value.Sign;\n    }\n\n    public static explicit operator long(Int256 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        if (value.Bytes0To8 > long.MaxValue)\n            throw new OverflowException();\n        return (long) value.Bytes0To8 * value.Sign;\n    }\n\n    public static explicit operator uint(Int256 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        if (value.Sign &lt; 0 || value.Bytes0To8 > uint.MaxValue)\n            throw new OverflowException();\n        return (uint) value.Bytes0To8;\n    }\n\n    public static explicit operator ushort(Int256 value)\n    {\n        if (value.Sign == 0)\n            return 0;\n        if (value.Sign &lt; 0 || value.Bytes0To8 > ushort.MaxValue)\n            throw new OverflowException();\n        return (ushort) value.Bytes0To8;\n    }\n\n    public static explicit operator ulong(Int256 value)\n    {\n        if (value.Sign &lt; 0 || value.Bytes8To16 != 0)\n            throw new OverflowException();\n        return value.Bytes0To8;\n    }\n\n    public static bool operator >(Int256 left, Int256 right)\n    {\n        return Compare(left, right) > 0;\n    }\n\n    public static bool operator &lt;(Int256 left, Int256 right)\n    {\n        return Compare(left, right) &lt; 0;\n    }\n\n    public static bool operator >=(Int256 left, Int256 right)\n    {\n        return Compare(left, right) >= 0;\n    }\n\n    public static bool operator &lt;=(Int256 left, Int256 right)\n    {\n        return Compare(left, right) &lt;= 0;\n    }\n\n    public static bool operator !=(Int256 left, Int256 right)\n    {\n        return Compare(left, right) != 0;\n    }\n\n    public static bool operator ==(Int256 left, Int256 right)\n    {\n        return Compare(left, right) == 0;\n    }\n\n    public static Int256 operator +(Int256 value)\n    {\n        return value;\n    }\n    public static Int256 operator ~(Int256 value)\n    {\n        return -(value + One);\n    }\n    public static Int256 operator -(Int256 value)\n    {\n        return Negate(value);\n    }\n\n    public static Int256 operator ++(Int256 value)\n    {\n        return value + 1;\n    }\n\n    public static Int256 operator --(Int256 value)\n    {\n        return value - 1;\n    }\n\n    public static Int256 Negate(Int256 value)\n    {\n        return new Int256(~value.Bytes24To32, ~value.Bytes16To24, ~value.Bytes8To16, ~value.Bytes0To8) + 1;\n    }\n\n    public Int256 ToAbs()\n    {\n        return Abs(this);\n    }\n\n    public static Int256 Abs(Int256 value)\n    {\n        if (value.Sign &lt; 0)\n            return -value;\n        return value;\n    }\n\n    public static Int256 operator +(Int256 left, Int256 right)\n    {\n        left.Bytes24To32 += right.Bytes24To32;\n        left.Bytes16To24 += right.Bytes16To24;\n        if (left.Bytes16To24 &lt; right.Bytes16To24)\n            left.Bytes24To32++;\n        left.Bytes8To16 += right.Bytes8To16;\n        if (left.Bytes8To16 &lt; right.Bytes8To16)\n        {\n            left.Bytes16To24++;\n            if (left.Bytes16To24 &lt; left.Bytes16To24 - 1)\n                left.Bytes24To32++;\n        }\n\n        left.Bytes0To8 += right.Bytes0To8;\n        if (left.Bytes0To8 &lt; right.Bytes0To8)\n        {\n            left.Bytes8To16++;\n            if (left.Bytes8To16 &lt; left.Bytes8To16 - 1)\n            {\n                left.Bytes16To24++;\n                if (left.Bytes16To24 &lt; left.Bytes16To24 - 1)\n                    left.Bytes24To32++;\n            }\n        }\n\n        return left;\n    }\n\n    public static Int256 operator -(Int256 left, Int256 right)\n    {\n        return left + -right;\n    }\n\n    public static Int256 Add(Int256 left, Int256 right)\n    {\n        return left + right;\n    }\n\n    public static Int256 Subtract(Int256 left, Int256 right)\n    {\n        return left - right;\n    }\n\n    public static Int256 Divide(Int256 dividend, Int256 divisor)\n    {\n        return DivRem(dividend, divisor, out var integer);\n    }\n\n    public static Int256 DivRem(Int256 dividend, Int256 divisor, out Int256 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 Int256(1, rem);\n        return new Int256(dividend.Sign * divisor.Sign, quotient);\n    }\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\n            remainder[0] = (uint) rem;\n            return;\n        }\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\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\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\n                    sum += normDividend[j + divisorLen];\n                    normDividend[j + divisorLen] = (uint) sum;\n                }\n            }\n\n            remainder = Unnormalize(normDividend, shift);\n            return;\n        }\n\n        quotient = new uint[0];\n        remainder = dividend;\n    }\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\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\n        if ((ui &amp; 0xff000000) == 0)\n        {\n            ui = ui &lt;&lt; 8;\n            shift += 8;\n        }\n\n        if ((ui &amp; 0xf0000000) == 0)\n        {\n            ui = ui &lt;&lt; 4;\n            shift += 4;\n        }\n\n        if ((ui &amp; 0xc0000000) == 0)\n        {\n            ui = ui &lt;&lt; 2;\n            shift += 2;\n        }\n\n        if ((ui &amp; 0x80000000) == 0)\n            shift++;\n        return shift;\n    }\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\n        return unnormalized;\n    }\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\n        while (i &lt; normalized.Length)\n            normalized[i++] = 0;\n        if (n != 0)\n            normalized[len] = n;\n    }\n\n    public static Int256 Remainder(Int256 dividend, Int256 divisor)\n    {\n        DivRem(dividend, divisor, out var remainder);\n        return remainder;\n    }\n\n    public static Int256 Max(Int256 left, Int256 right)\n    {\n        return left.CompareTo(right) &lt; 0 ? right : left;\n    }\n\n    public static Int256 Min(Int256 left, Int256 right)\n    {\n        return left.CompareTo(right) &lt;= 0 ? left : right;\n    }\n\n    public static int GetBitWidth(Int256 n)\n    {\n        Int256 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\n    public static Int256 operator %(Int256 dividend, Int256 divisor)\n    {\n        return Remainder(dividend, divisor);\n    }\n\n    public static Int256 operator \/(Int256 dividend, Int256 divisor)\n    {\n        return Divide(dividend, divisor);\n    }\n\n    public ulong[] ToUIn64Array()\n    {\n        return new[] {Bytes0To8, Bytes8To16, Bytes16To24, Bytes24To32};\n    }\n\n    public uint[] ToUIn32Array()\n    {\n        var uia = new uint[8];\n        var ula = ToUIn64Array();\n        Buffer.BlockCopy(ula, 0, uia, 0, 32);\n        return uia;\n    }\n\n    public byte[] ToByteArray()\n    {\n        var ba = new byte[32];\n        var ula = ToUIn64Array();\n        Buffer.BlockCopy(ula, 0, ba, 0, 32);\n        return ba;\n    }\n\n    public static Int256 Multiply(Int256 left, Int256 right)\n    {\n        var xInts = left.ToUIn32Array();\n        var yInts = right.ToUIn32Array();\n        var mulInts = new uint[16];\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\n            while (remainder != 0)\n            {\n                remainder += mulInts[index];\n                mulInts[index++] = (uint) remainder;\n                remainder = remainder >> 32;\n            }\n        }\n\n        return new Int256(left.Sign * right.Sign, mulInts);\n    }\n\n    public static Int256 operator *(Int256 left, Int256 right)\n    {\n        return Multiply(left, right);\n    }\n\n    public static Int256 operator >>(Int256 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 digitShift = shift \/ 32;\n        var smallShift = shift - digitShift * 32;\n        var xd = value.ToUIn32Array();\n        var xl = xd.Length;\n        if (value.Sign &lt; 0)\n        {\n            if (shift >= 32 * xl)\n                return new Int256(-1);\n            var zd = new uint[xl];\n            Array.Copy(xd, zd, xl);\n            xd = zd;\n            TwosComplement(xd);\n        }\n\n        var length = xl - digitShift;\n        if (length &lt; 0)\n            length = 0;\n        var d = new uint[length];\n        if (smallShift == 0)\n        {\n            for (var index = xl - 1; index >= digitShift; --index)\n                d[index - digitShift] = xd[index];\n        }\n        else\n        {\n            var carryShift = 32 - smallShift;\n            uint carry = 0;\n            for (var index = xl - 1; index >= digitShift; --index)\n            {\n                var rot = xd[index];\n                d[index - digitShift] = !(value.Sign &lt; 0) || index != xl - 1\n                    ? (rot >> smallShift) | carry\n                    : (rot >> smallShift) | (uint) (-1 &lt;&lt; carryShift);\n                carry = rot &lt;&lt; carryShift;\n            }\n        }\n\n        if (value.Sign &lt; 0)\n            TwosComplement(d);\n        return new Int256(value.Sign, d);\n    }\n\n    private static void TwosComplement(uint[] d)\n    {\n        uint v = 0;\n        var i = 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\n        if (v != 0)\n        {\n            for (; i &lt; d.Length; i++)\n                d[i] = ~d[i];\n        }\n        else\n        {\n            var len = d.Length + 1;\n            var r = new uint[len];\n            var n = Math.Min(d.Length, len);\n            for (var j = 0; j &lt; n; j++)\n                r[j] = d[j];\n            d = r;\n            d[d.Length - 1] = 1;\n        }\n    }\n\n    public static Int256 operator &lt;&lt;(Int256 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.ToUIn32Array();\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\n            zd[index + digitShift] = carry;\n        }\n\n        return new Int256(value.Sign, zd);\n    }\n\n    public static Int256 operator |(Int256 left, Int256 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\n        return new Int256(left.Sign * right.Sign, z);\n    }\n\n    public static Int256 operator ^(Int256 left, Int256 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\n        return new Int256(left.Sign * right.Sign, z);\n    }\n\n    public static Int256 operator &amp;(Int256 left, Int256 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\n        return new Int256(left.Sign * right.Sign, z);\n    }\n\n    public class Int256Converter : TypeConverter\n    {\n        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)\n        {\n            return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);\n        }\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 Int256();\n        }\n\n        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)\n        {\n            return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);\n        }\n\n        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value,\n            Type destinationType)\n        {\n            return destinationType == typeof(string)\n                ? $\"{value}\"\n                : base.ConvertTo(context, culture, value, destinationType);\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Int256 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":[89,5,88],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/175"}],"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=175"}],"version-history":[{"count":3,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":444,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/175\/revisions\/444"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}