{"id":181,"date":"2020-08-11T05:54:22","date_gmt":"2020-08-11T05:54:22","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=181"},"modified":"2021-06-12T03:07:25","modified_gmt":"2021-06-12T03:07:25","slug":"fixeduintxint-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/08\/11\/fixeduintxint-cs\/","title":{"rendered":"(Obsolete) FixedUIntXInt.cs"},"content":{"rendered":"\n<p>Variable Bit Width Unsigned Integer 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=\"\">[Serializable]\n    [StructLayout(LayoutKind.Sequential, Pack = 1)]\n    [TypeConverter(typeof(FixedUIntXIntConverter))]\n    [DebuggerDisplay(\"{DDisplay}\")]\n    public class FixedUIntXInt : IComparable&lt;FixedUIntXInt>, IComparable, IEquatable&lt;FixedUIntXInt>, IConvertible, IFormattable\n    {\n        private const  int           DataSize      = sizeof(uint);\n        private static int           DataBitLength = 64; \/\/&lt;--Change Here\n        private static int           DataLength    = DataBitLength >> 5;\n        public static  FixedUIntXInt Zero          = new FixedUIntXInt(\"0\",  64);\n        public static  FixedUIntXInt Ten           = new FixedUIntXInt(\"10\", 64);\n        public static  FixedUIntXInt One           = new FixedUIntXInt(\"1\",  64);\n        private        uint[]        data;\n        public FixedUIntXInt(FixedUIntXInt value)\n        {\n            if (value == null)\n                return;\n            CalculateMinDataLength(value.data.Length);\n            data = new uint[DataLength];\n            value.data.CopyTo(data, 0);\n        }\n        public FixedUIntXInt(string value, int orBl)\n        {\n            if (orBl > 0)\n                CalculateMinDataLength(orBl >> 5);\n            if (!TryParse(value, out var result))\n                throw new Exception(\"TryParse Failed.\");\n            data = new uint[DataLength];\n            result.data.CopyTo(data, 0);\n        }\n        public FixedUIntXInt(byte value)\n        {\n            data    = new uint[DataLength];\n            data[0] = value;\n        }\n        public FixedUIntXInt(bool value)\n        {\n            data    = new uint[DataLength];\n            data[0] = (uint) (value ? 1 : 0);\n        }\n        public FixedUIntXInt(char value)\n        {\n            data    = new uint[DataLength];\n            data[0] = value;\n        }\n        public FixedUIntXInt(BigDecimal value)\n        {\n            var ba = value.UnscaledValue.ToByteArray();\n            data = new uint[DataLength];\n            for (var i = 0; i &lt; DataLength; i++)\n                data[i] = BitConverter.ToUInt32(ba, i * DataSize);\n        }\n        public FixedUIntXInt(decimal value)\n        {\n            data = new uint[DataLength];\n            if (value &lt; 0)\n            {\n                var n = -new FixedUIntXInt(-value);\n                n.data.CopyTo(data, 0);\n                return;\n            }\n            var bits = decimal.GetBits(value);\n            data[2] = (uint) bits[2];\n            data[1] = (uint) bits[1];\n            data[0] = (uint) bits[0];\n        }\n        public FixedUIntXInt(double value) : this((decimal) value)\n        {\n        }\n        public FixedUIntXInt(float value) : this((decimal) value)\n        {\n        }\n        public FixedUIntXInt(short value) : this((int) value)\n        {\n        }\n        public FixedUIntXInt(int value)\n        {\n            data    = new uint[DataLength];\n            data[0] = (uint) value;\n        }\n        public FixedUIntXInt(long value) : this((ulong) value)\n        {\n        }\n        public FixedUIntXInt(sbyte value) : this((int) value)\n        {\n        }\n        public FixedUIntXInt(ushort value)\n        {\n            data    = new uint[DataLength];\n            data[0] = value;\n        }\n        public FixedUIntXInt(uint value)\n        {\n            data    = new uint[DataLength];\n            data[0] = value;\n        }\n        public FixedUIntXInt(ulong value)\n        {\n            data    = new uint[DataLength];\n            data[0] = (uint) ((value >> 32) &amp; 0xffffffff);\n            data[1] = (uint) (value         &amp; 0xffffffff);\n        }\n        public FixedUIntXInt(BigInteger value) : this(value.ToByteArray())\n        {\n        }\n        public FixedUIntXInt(Guid value) : this(value.ToByteArray())\n        {\n        }\n        public FixedUIntXInt(byte[] value)\n        {\n            var minSize = value.Length \/ DataSize;\n            if (value == null)\n                throw new ArgumentNullException(\"value\");\n            var byteCount      = value.Length;\n            var isNegative     = byteCount > 0 &amp;&amp; (value[byteCount - 1] &amp; 0x80) == 0x80;\n            var unalignedBytes = byteCount % DataSize;\n            var dwordCount     = byteCount \/ DataSize + (unalignedBytes == 0 ? 0 : 1);\n            data = new uint[Math.Max(dwordCount, minSize)];\n            if (byteCount == 0)\n                return;\n            int curDword, curByte, byteInDword;\n            curByte = 3;\n            for (curDword = 0; curDword &lt; dwordCount - (unalignedBytes == 0 ? 0 : 1); curDword++)\n            {\n                byteInDword = 0;\n                while (byteInDword &lt; DataSize)\n                {\n                    data[curDword] &lt;&lt;= 8;\n                    data[curDword] |=  value[curByte];\n                    curByte--;\n                    byteInDword++;\n                }\n                curByte += 8;\n            }\n            if (unalignedBytes != 0)\n            {\n                if (isNegative)\n                    data[dwordCount - 1] = 0xffffffff;\n                for (curByte = byteCount - 1; curByte >= byteCount - unalignedBytes; curByte--)\n                {\n                    data[curDword] &lt;&lt;= 8;\n                    data[curDword] |=  value[curByte];\n                }\n            }\n        }\n        public FixedUIntXInt(uint[] array)\n        {\n            if (array == null)\n                throw new Exception(\"Array cannot be null.\");\n            if (array.Length != DataLength)\n                Array.Resize(ref array, DataLength);\n            data = new uint[DataLength];\n            var ba = new byte[4];\n            for (var i = 0; i &lt; DataLength; i++)\n            {\n                Array.Copy(BitConverter.GetBytes(array[i]), 0, ba, 0, DataSize);\n                data[i] = BitConverter.ToUInt32(ba, 0);\n            }\n        }\n        [DebuggerBrowsable(DebuggerBrowsableState.Never)]\n        private string DDisplay => ToString();\n        public FixedUIntXInt MaxValue => (One &lt;&lt; DataBitLength) - 1;\n        public int SizeOfDataUsed\n        {\n            get\n            {\n                var dataUsed = data.Length;\n                while (dataUsed > 1 &amp;&amp; data[dataUsed - 1] == 0)\n                    --dataUsed;\n                if (dataUsed == 0)\n                    dataUsed = 1;\n                return dataUsed;\n            }\n        }\n        public int ResetDataBitLength\n        {\n            get => DataBitLength;\n            set\n            {\n                if (DataBitLength == value)\n                    return;\n                DataBitLength = value;\n                DataLength    = DataBitLength >> 5;\n                Array.Resize(ref data, DataLength);\n            }\n        }\n        public int BitWidth\n        {\n            get\n            {\n                FixedUIntXInt bw = 1;\n                var           v  = this;\n                while ((v >>= 1) > 0)\n                    bw++;\n                if (bw &lt; 8)\n                    bw = 8;\n                while (bw % 8 != 0)\n                    bw++;\n                return (int) bw;\n            }\n        }\n        public bool IsOne  => this     == One;\n        public bool IsEven => this % 2 == 0;\n        public bool IsZero\n        {\n            get\n            {\n                for (var i = 0; i &lt; data.Length; i++)\n                    if (data[i] != 0)\n                        return false;\n                return true;\n            }\n        }\n        int IComparable.CompareTo(object obj)\n        {\n            return Compare(this, obj);\n        }\n        public int CompareTo(FixedUIntXInt value)\n        {\n            return Compare(this, value);\n        }\n        TypeCode IConvertible.GetTypeCode()\n        {\n            return TypeCode.Object;\n        }\n        bool IConvertible.ToBoolean(IFormatProvider provider)\n        {\n            return (bool) this;\n        }\n        byte IConvertible.ToByte(IFormatProvider provider)\n        {\n            return (byte) this;\n        }\n        char IConvertible.ToChar(IFormatProvider provider)\n        {\n            return (char) this;\n        }\n        DateTime IConvertible.ToDateTime(IFormatProvider provider)\n        {\n            throw new InvalidCastException();\n        }\n        decimal IConvertible.ToDecimal(IFormatProvider provider)\n        {\n            return (decimal) this;\n        }\n        double IConvertible.ToDouble(IFormatProvider provider)\n        {\n            return (double) this;\n        }\n        short IConvertible.ToInt16(IFormatProvider provider)\n        {\n            return (short) this;\n        }\n        int IConvertible.ToInt32(IFormatProvider provider)\n        {\n            return (int) this;\n        }\n        long IConvertible.ToInt64(IFormatProvider provider)\n        {\n            return (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 object ToType(Type conversionType, IFormatProvider provider)\n        {\n            object value;\n            if (TryConvert(conversionType, provider, out value))\n                return value;\n            throw new InvalidCastException();\n        }\n        ushort IConvertible.ToUInt16(IFormatProvider provider)\n        {\n            if (data[1] != 0)\n                throw new OverflowException();\n            return Convert.ToUInt16(data[0]);\n        }\n        uint IConvertible.ToUInt32(IFormatProvider provider)\n        {\n            if (data[1] != 0)\n                throw new OverflowException();\n            return Convert.ToUInt32(data[0]);\n        }\n        ulong IConvertible.ToUInt64(IFormatProvider provider)\n        {\n            if (data[1] != 0)\n                throw new OverflowException();\n            return data[0];\n        }\n        public bool Equals(FixedUIntXInt obj)\n        {\n            if (ReferenceEquals(obj, null))\n                return false;\n            if (ReferenceEquals(this, obj))\n                return true;\n            if (data.Length != obj.data.Length)\n            {\n                var len = Math.Max(data.Length, obj.data.Length);\n                if (data.Length &lt; len)\n                {\n                    var tData = new uint[len];\n                    Array.Copy(data, 0, tData, 0, data.Length);\n                    data = tData;\n                }\n                if (obj.data.Length &lt; len)\n                    Resize(ref obj, len);\n            }\n            for (var i = 0; i &lt; data.Length; i++)\n                if (data[i] != obj.data[i])\n                    return false;\n            return true;\n        }\n        public string ToString(string format, IFormatProvider formatProvider)\n        {\n            if (formatProvider == null)\n                formatProvider = CultureInfo.CurrentCulture;\n            if (!string.IsNullOrEmpty(format))\n            {\n                var ch = format[0];\n                if (ch == 'x' || ch == 'X')\n                {\n                    int.TryParse(format.Substring(1).Trim(), out var min);\n                    return ToHexString(ch == 'X', 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)), 10);\n        }\n        private static void CalculateMinDataLength(int minSize)\n        {\n            if (minSize != DataLength)\n            {\n                DataBitLength = 32 * minSize;\n                DataLength    = minSize;\n            }\n        }\n        private static byte[] ToByteArray(ulong[] value)\n        {\n            var ba = new byte[value.Length &lt;&lt; 3];\n            Buffer.BlockCopy(value, 0, ba, 0, value.Length &lt;&lt; 3);\n            return ba;\n        }\n        private static byte[] ToByteArray(uint[] value)\n        {\n            var ba = new byte[value.Length &lt;&lt; 2];\n            Buffer.BlockCopy(value, 0, ba, 0, value.Length &lt;&lt; 2);\n            return ba;\n        }\n        public override int GetHashCode()\n        {\n            var hash = 0x811c9dc5;\n            for (var i = 0; i &lt; DataLength; i++)\n            {\n                hash ^= ((hash &lt;&lt; 13) | (hash >> 19)) ^ data[i];\n                hash *= 0x1000193;\n            }\n            return (int) hash;\n        }\n        public override bool Equals(object obj)\n        {\n            return base.Equals(obj);\n        }\n        public override string ToString()\n        {\n            return ToString(null, null);\n        }\n        public string ToString(string format)\n        {\n            return ToString(format, null);\n        }\n        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, int radix)\n        {\n            if (radix &lt; 2 || radix > 36)\n                throw new ArgumentOutOfRangeException(\"radix\");\n            if (IsZero)\n                return \"0\";\n            var          a       = new FixedUIntXInt(this);\n            var          biRadix = new FixedUIntXInt(radix);\n            const string charSet = \"0123456789abcdefghijklmnopqrstuvwxyz\";\n            var          al      = new ArrayList();\n            while (a > 0)\n                try\n                {\n                    Divide(a, biRadix, out var remainder, out var quotient);\n                    al.Insert(0, charSet[(int) remainder.data[0]]);\n                    a = quotient;\n                }\n                catch (Exception ex)\n                {\n                    break;\n                }\n            var result = new string((char[]) al.ToArray(typeof(char)));\n            return result;\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 FixedUIntXInt Parse(string value)\n        {\n            return Parse(value, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);\n        }\n        public static FixedUIntXInt Parse(string value, NumberStyles style)\n        {\n            return Parse(value, style, NumberFormatInfo.CurrentInfo);\n        }\n        public static FixedUIntXInt Parse(string value, IFormatProvider provider)\n        {\n            return Parse(value, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));\n        }\n        public static FixedUIntXInt 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 FixedUIntXInt 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 FixedUIntXInt 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 TryParseNum(value, 16, out result);\n            return TryParseNum(value, 10, out result);\n        }\n        public static bool TryParseNum(string digits, int radix, out FixedUIntXInt result)\n        {\n            result = new FixedUIntXInt(0);\n            if (digits == null)\n                return false;\n            var multiplier = new FixedUIntXInt(1);\n            digits = digits.ToUpper(CultureInfo.CurrentCulture).Trim();\n            var nDigits = digits[0] == '-' ? 1 : 0;\n            for (var idx = digits.Length - 1; idx >= nDigits; idx--)\n            {\n                var d = (int) digits[idx];\n                if (d >= '0' &amp;&amp; d &lt;= '9')\n                    d -= '0';\n                else if (d >= 'A' &amp;&amp; d &lt;= 'Z')\n                    d = d - 'A' + 10;\n                else\n                    return false;\n                if (d >= radix)\n                    return false;\n                result     += multiplier * d;\n                multiplier *= radix;\n            }\n            if (digits[0] == '-')\n                result = -result;\n            return true;\n        }\n        public static int Compare(FixedUIntXInt left, object right)\n        {\n            if (right is FixedUIntXInt)\n                return Compare(left, (FixedUIntXInt) right);\n            if (right is bool)\n                return Compare(left, new FixedUIntXInt((bool) right));\n            if (right is byte)\n                return Compare(left, new FixedUIntXInt((byte) right));\n            if (right is char)\n                return Compare(left, new FixedUIntXInt((char) right));\n            if (right is decimal)\n                return Compare(left, new FixedUIntXInt((decimal) right));\n            if (right is double)\n                return Compare(left, new FixedUIntXInt((double) right));\n            if (right is short)\n                return Compare(left, new FixedUIntXInt((short) right));\n            if (right is int)\n                return Compare(left, new FixedUIntXInt((int) right));\n            if (right is long)\n                return Compare(left, new FixedUIntXInt((long) right));\n            if (right is sbyte)\n                return Compare(left, new FixedUIntXInt((sbyte) right));\n            if (right is float)\n                return Compare(left, new FixedUIntXInt((float) right));\n            if (right is ushort)\n                return Compare(left, new FixedUIntXInt((ushort) right));\n            if (right is uint)\n                return Compare(left, new FixedUIntXInt((uint) right));\n            if (right is ulong)\n                return Compare(left, new FixedUIntXInt((ulong) right));\n            var bytes = right as byte[];\n            if (bytes != null &amp;&amp; bytes.Length != 64)\n                return Compare(left, new FixedUIntXInt(bytes));\n            if (right is Guid)\n                return Compare(left, new FixedUIntXInt((Guid) right));\n            throw new ArgumentException();\n        }\n        public static int Compare(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            MakeLikeLengths(ref left, ref right);\n            if (ReferenceEquals(left, right))\n                return 0;\n            if (ReferenceEquals(left, null))\n                throw new ArgumentNullException(\"left\");\n            if (ReferenceEquals(right, null))\n                throw new ArgumentNullException(\"right\");\n            if (left > right) return 1;\n            if (left == right) return 0;\n            return -1;\n        }\n        public static implicit operator FixedUIntXInt(bool value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static implicit operator FixedUIntXInt(byte value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static implicit operator FixedUIntXInt(char value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static explicit operator FixedUIntXInt(decimal value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static explicit operator FixedUIntXInt(double value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static implicit operator FixedUIntXInt(short value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static implicit operator FixedUIntXInt(int value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static implicit operator FixedUIntXInt(long value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static implicit operator FixedUIntXInt(sbyte value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static explicit operator FixedUIntXInt(float value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static implicit operator FixedUIntXInt(ushort value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static implicit operator FixedUIntXInt(uint value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static implicit operator FixedUIntXInt(ulong value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static implicit operator FixedUIntXInt(BigInteger value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static implicit operator FixedUIntXInt(BigDecimal value)\n        {\n            return new FixedUIntXInt(value);\n        }\n        public static explicit operator bool(FixedUIntXInt value)\n        {\n            return (byte) value.data[0] != 0;\n        }\n        public static explicit operator byte(FixedUIntXInt value)\n        {\n            return (byte) value.data[0];\n        }\n        public static explicit operator char(FixedUIntXInt value)\n        {\n            return (char) (ushort) value.data[0];\n        }\n        public static explicit operator decimal(FixedUIntXInt value)\n        {\n            return new decimal((int) value.data[0], (int) value.data[1], (int) value.data[2], false, 0);\n        }\n        public static explicit operator double(FixedUIntXInt value)\n        {\n            var nfi = CultureInfo.InvariantCulture.NumberFormat;\n            if (!double.TryParse(value.ToString(nfi, 10), NumberStyles.Number, nfi, out var d))\n                throw new OverflowException();\n            return d;\n        }\n        public static explicit operator float(FixedUIntXInt value)\n        {\n            var nfi = CultureInfo.InvariantCulture.NumberFormat;\n            if (!float.TryParse(value.ToString(nfi, 10), NumberStyles.Number, nfi, out var f))\n                throw new OverflowException();\n            return f;\n        }\n        public static explicit operator short(FixedUIntXInt value)\n        {\n            return (short) (int) value.data[0];\n        }\n        public static explicit operator int(FixedUIntXInt value)\n        {\n            return (int) value.data[0];\n        }\n        public static explicit operator long(FixedUIntXInt value)\n        {\n            if (value.data[1] != 0)\n                return (long) (((ulong) value.data[1] &lt;&lt; 32) | value.data[0]);\n            return value.data[0];\n        }\n        public static explicit operator uint(FixedUIntXInt value)\n        {\n            return value.data[0];\n        }\n        public static explicit operator ushort(FixedUIntXInt value)\n        {\n            return (ushort) value.data[0];\n        }\n        public static explicit operator ulong(FixedUIntXInt value)\n        {\n            if (value.data[1] != 0)\n                return ((ulong) value.data[1] &lt;&lt; 32) | value.data[0];\n            return value.data[0];\n        }\n        public static explicit operator BigInteger(FixedUIntXInt value)\n        {\n            return new BigInteger(value.ToByteArray());\n        }\n        public static bool operator >(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            MakeLikeLengths(ref left, ref right);\n            if (ReferenceEquals(left, null))\n                throw new ArgumentNullException(\"left\");\n            if (ReferenceEquals(right, null))\n                throw new ArgumentNullException(\"right\");\n            for (var i = 0; i &lt; DataLength; i++)\n                if (left.data[i] != right.data[i])\n                    return left.data[i] > right.data[i];\n            return false;\n        }\n        private static void MakeLikeLengths(ref FixedUIntXInt left, ref FixedUIntXInt right)\n        {\n            if (left.data.Length != right.data.Length)\n            {\n                var len = Math.Max(left.data.Length, right.data.Length);\n                Resize(ref left,  len);\n                Resize(ref right, len);\n            }\n        }\n        private static void Resize(ref FixedUIntXInt value, int newSize)\n        {\n            var nData = new uint[newSize];\n            var len   = value.data.Length;\n            for (var i = 0; i &lt; len; i++)\n                nData[i] = value.data[i];\n            value.data = (uint[]) nData.Clone();\n        }\n        public static bool operator &lt;(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            return Compare(left, right) &lt; 0;\n        }\n        public static bool operator >=(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            return Compare(left, right) >= 0;\n        }\n        public static bool operator &lt;=(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            return Compare(left, right) &lt;= 0;\n        }\n        public static bool operator !=(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            return Compare(left, right) != 0;\n        }\n        public static bool operator ==(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            if (ReferenceEquals(left, right))\n                return true;\n            if (ReferenceEquals(left, null) || ReferenceEquals(right, null))\n                return false;\n            return left.Equals(right);\n        }\n        public static FixedUIntXInt operator +(FixedUIntXInt value)\n        {\n            return value;\n        }\n        public static FixedUIntXInt operator ~(FixedUIntXInt value)\n        {\n            var da = new uint[DataLength];\n            for (var idx = 0; idx &lt; DataLength; idx++)\n                da[idx] = ~value.data[idx];\n            return new FixedUIntXInt(da);\n        }\n        public static FixedUIntXInt operator -(FixedUIntXInt value)\n        {\n            return Negate(value);\n        }\n        public static FixedUIntXInt operator ++(FixedUIntXInt value)\n        {\n            return value + 1;\n        }\n        public static FixedUIntXInt operator --(FixedUIntXInt value)\n        {\n            return value - 1;\n        }\n        public static FixedUIntXInt Negate(FixedUIntXInt value)\n        {\n            for (var i = 0; i &lt; DataLength; i++)\n                value.data[i] = ~value.data[i];\n            return new FixedUIntXInt(value);\n        }\n        public static FixedUIntXInt operator +(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            var  dl     = left.data.Length > right.data.Length ? left.data.Length : right.data.Length;\n            var  result = new uint[dl];\n            long carry  = 0;\n            for (var i = 0; i &lt; dl; i++)\n            {\n                var sum = left.data[i] + (long) right.data[i] + carry;\n                carry     = sum >> 32;\n                result[i] = (uint) (sum &amp; 0xFFFFFFFF);\n            }\n            if (carry != 0)\n            {\n                var idx = 0;\n                while (idx &lt; result.Length - 1)\n                {\n                    if (result[idx] == 0)\n                        break;\n                    idx++;\n                }\n                result[idx] = (uint) carry;\n            }\n            return new FixedUIntXInt(result);\n        }\n        public static FixedUIntXInt operator -(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            var  size  = Math.Max(left.data.Length, right.data.Length) + 1;\n            var  da    = new uint[size];\n            long carry = 0;\n            for (var i = 0; i &lt; da.Length - 1; i++)\n            {\n                var diff = left.data[i] - (long) right.data[i] - carry;\n                da[i] = (uint) (diff &amp; DigitsArray.AllBits);\n                carry = diff &lt; 0 ? 1 : 0;\n            }\n            return new FixedUIntXInt(da);\n        }\n        public static FixedUIntXInt Add(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            return left + right;\n        }\n        public static FixedUIntXInt Subtract(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            return left - right;\n        }\n        public static FixedUIntXInt Divide(FixedUIntXInt dividend, FixedUIntXInt divisor)\n        {\n            return DivRem(dividend, divisor, out var integer);\n        }\n        public static void Divide(FixedUIntXInt dividend, FixedUIntXInt divisor, out FixedUIntXInt remainder, out FixedUIntXInt quotient)\n        {\n            if (divisor == 0)\n                throw new DivideByZeroException();\n            DivRem(dividend.data, divisor.data, out var quo, out var rem);\n            remainder = new FixedUIntXInt(rem);\n            quotient  = new FixedUIntXInt(quo);\n        }\n        public static FixedUIntXInt DivRem(FixedUIntXInt dividend, FixedUIntXInt divisor, out FixedUIntXInt remainder)\n        {\n            if (divisor == 0)\n                throw new DivideByZeroException();\n            DivRem(dividend.data, divisor.data, out var quotient, out var rem);\n            remainder = new FixedUIntXInt(rem);\n            return new FixedUIntXInt(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 ((long) dj &lt; 0)\n                    {\n                        quotient[j]--;\n                        ulong sum = 0;\n                        for (index = 0; index &lt; divisorLen; index++)\n                        {\n                            sum                     = normDivisor[index] + normDividend[j + index] + sum;\n                            normDividend[j + index] = (uint) sum;\n                            sum                     = sum >> 32;\n                        }\n                        sum += normDividend[j + divisorLen];\n                        normDividend[j        + divisorLen] = (uint) sum;\n                    }\n                }\n                remainder = Unnormalize(normDividend, shift);\n                return;\n            }\n            quotient  = new uint[1];\n            remainder = dividend;\n        }\n        private static int GetLength(uint[] uints)\n        {\n            var index = uints.Length - 1;\n            while (index >= 0 &amp;&amp; uints[index] == 0)\n                index--;\n            return index + 1;\n        }\n        private static int GetNormalizeShift(uint ui)\n        {\n            var shift = 0;\n            if ((ui &amp; 0xffff0000) == 0)\n            {\n                ui    =  ui &lt;&lt; 16;\n                shift += 16;\n            }\n            if ((ui &amp; 0xff000000) == 0)\n            {\n                ui    =  ui &lt;&lt; 8;\n                shift += 8;\n            }\n            if ((ui &amp; 0xf0000000) == 0)\n            {\n                ui    =  ui &lt;&lt; 4;\n                shift += 4;\n            }\n            if ((ui &amp; 0xc0000000) == 0)\n            {\n                ui    =  ui &lt;&lt; 2;\n                shift += 2;\n            }\n            if ((ui &amp; 0x80000000) == 0)\n                shift++;\n            return shift;\n        }\n        private static uint[] Unnormalize(uint[] normalized, int shift)\n        {\n            var len          = GetLength(normalized);\n            var unnormalized = new uint[len];\n            if (shift > 0)\n            {\n                var  rshift = 32 - shift;\n                uint r      = 0;\n                for (var i = len - 1; i >= 0; i--)\n                {\n                    unnormalized[i] = (normalized[i] >> shift) | r;\n                    r               = normalized[i] &lt;&lt; rshift;\n                }\n            }\n            else\n            {\n                for (var j = 0; j &lt; len; j++)\n                    unnormalized[j] = normalized[j];\n            }\n            return unnormalized;\n        }\n        private static void Normalize(uint[] unormalized, int len, uint[] normalized, int shift)\n        {\n            int  i;\n            uint n = 0;\n            if (shift > 0)\n            {\n                var rShift = 32 - shift;\n                for (i = 0; i &lt; len; i++)\n                {\n                    normalized[i] = (unormalized[i] &lt;&lt; shift) | n;\n                    n             = unormalized[i] >> rShift;\n                }\n            }\n            else\n            {\n                i = 0;\n                while (i &lt; len)\n                {\n                    normalized[i] = unormalized[i];\n                    i++;\n                }\n            }\n            while (i &lt; normalized.Length)\n                normalized[i++] = 0;\n            if (n != 0)\n                normalized[len] = n;\n        }\n        public static FixedUIntXInt Remainder(FixedUIntXInt dividend, FixedUIntXInt divisor)\n        {\n            DivRem(dividend, divisor, out var remainder);\n            return remainder;\n        }\n        public static FixedUIntXInt Max(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            return left.CompareTo(right) &lt; 0 ? right : left;\n        }\n        public static FixedUIntXInt Min(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            return left.CompareTo(right) &lt;= 0 ? left : right;\n        }\n        public static int GetBitWidth(FixedUIntXInt n)\n        {\n            FixedUIntXInt bw = 1;\n            var           v  = n;\n            while ((v >>= 1) > 0)\n                bw++;\n            if (bw &lt; 8)\n                bw = 8;\n            while (bw % 8 != 0)\n                bw++;\n            return (int) bw;\n        }\n        public static FixedUIntXInt operator %(FixedUIntXInt dividend, FixedUIntXInt divisor)\n        {\n            return Remainder(dividend, divisor);\n        }\n        public static FixedUIntXInt operator \/(FixedUIntXInt dividend, FixedUIntXInt divisor)\n        {\n            return Divide(dividend, divisor);\n        }\n        public ulong[] ToUIn64Array()\n        {\n            var al = data.Length >> 1;\n            if (al * 2 != data.Length)\n                al++;\n            var arr = new ulong[al];\n            Buffer.BlockCopy(data, 0, arr, 0, data.Length &lt;&lt; 2);\n            return arr;\n        }\n        public uint[] ToUIn32Array()\n        {\n            return data;\n        }\n        public byte[] ToByteArray()\n        {\n            var ba = new byte[data.Length * DataSize];\n            Buffer.BlockCopy(data, 0, ba, 0, data.Length * DataSize);\n            return ba;\n        }\n        public byte[] ToByteArray(int length)\n        {\n            if (length &lt;= 0 || length > data.Length * DataSize)\n                throw new ArgumentException($\"Length {length} out of range length > 0 or length &lt;= {data.Length * DataSize}\");\n            var ba = new byte[length];\n            Buffer.BlockCopy(data, 0, ba, 0, length);\n            return ba;\n        }\n        public static FixedUIntXInt Multiply(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            var xInts   = left.data;\n            var yInts   = right.data;\n            var mulInts = new uint[Math.Max(xInts.Length, yInts.Length) &lt;&lt; 1];\n            for (var i = 0; i &lt; xInts.Length; i++)\n            {\n                var   index     = i;\n                ulong remainder = 0;\n                foreach (var yi in yInts)\n                {\n                    remainder        = remainder + (ulong) xInts[i] * yi + mulInts[index];\n                    mulInts[index++] = (uint) remainder;\n                    remainder        = remainder >> 32;\n                }\n                while (remainder != 0)\n                {\n                    remainder        += mulInts[index];\n                    mulInts[index++] =  (uint) remainder;\n                    remainder        =  remainder >> 32;\n                }\n            }\n            return new FixedUIntXInt(mulInts);\n        }\n        public static FixedUIntXInt operator *(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            return Multiply(left, right);\n        }\n        public static FixedUIntXInt operator >>(FixedUIntXInt value, int shift)\n        {\n            if (value == Zero)\n                return Zero;\n            var xd          = (uint[]) value.data.Clone();\n            var shiftAmount = 32;\n            var invShift    = 0;\n            var bufLen      = xd.Length;\n            while (bufLen > 1 &amp;&amp; xd[bufLen - 1] == 0)\n                bufLen--;\n            for (var count = shift; count > 0; count -= shiftAmount)\n            {\n                if (count &lt; shiftAmount)\n                {\n                    shiftAmount = count;\n                    invShift    = 32 - shiftAmount;\n                }\n                ulong carry = 0;\n                for (var i = bufLen - 1; i >= 0; i--)\n                {\n                    var val = (ulong) xd[i] >> shiftAmount;\n                    val   |= carry;\n                    carry =  (ulong) xd[i] &lt;&lt; invShift;\n                    xd[i] =  (uint) val;\n                }\n            }\n            return new FixedUIntXInt(xd);\n        }\n        public static FixedUIntXInt operator &lt;&lt;(FixedUIntXInt value, int shift)\n        {\n            if (value == Zero)\n                return Zero;\n            var digitShift = shift \/ 32;\n            var smallShift = shift - digitShift * 32;\n            var xd         = (uint[]) value.data.Clone();\n            var xl         = xd.Length;\n            var zd         = new uint[xl + digitShift + 1];\n            if (smallShift == 0)\n            {\n                for (var index = 0; index &lt; xl; ++index)\n                    zd[index + digitShift] = xd[index];\n            }\n            else\n            {\n                var  carryShift = 32 - smallShift;\n                uint carry      = 0;\n                int  index;\n                for (index = 0; index &lt; xl; ++index)\n                {\n                    var rot = xd[index];\n                    zd[index + digitShift] = (rot &lt;&lt; smallShift) | carry;\n                    carry                  = rot >> carryShift;\n                }\n                zd[index + digitShift] = carry;\n            }\n            return new FixedUIntXInt(zd);\n        }\n        public static FixedUIntXInt operator |(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            if (left == 0)\n                return right;\n            if (right == 0)\n                return left;\n            var z = new uint[Math.Max(left.data.Length, right.data.Length)];\n            for (var i = 0; i &lt; z.Length; i++)\n            {\n                var xu = i &lt; left.data.Length ? left.data[i] : 0U;\n                var yu = i &lt; right.data.Length ? right.data[i] : 0U;\n                z[i] = xu | yu;\n            }\n            return new FixedUIntXInt(z);\n        }\n        public static FixedUIntXInt operator ^(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            var z = new uint[Math.Max(left.data.Length, right.data.Length)];\n            for (var i = 0; i &lt; z.Length; i++)\n            {\n                var xu = i &lt; left.data.Length ? left.data[i] : 0U;\n                var yu = i &lt; right.data.Length ? right.data[i] : 0U;\n                z[i] = xu ^ yu;\n            }\n            return new FixedUIntXInt(z);\n        }\n        public static FixedUIntXInt operator &amp;(FixedUIntXInt left, FixedUIntXInt right)\n        {\n            if (left == 0 || right == 0)\n                return Zero;\n            var z = new uint[Math.Max(left.data.Length, right.data.Length)];\n            for (var i = 0; i &lt; z.Length; i++)\n            {\n                var xu = i &lt; left.data.Length ? left.data[i] : 0U;\n                var yu = i &lt; right.data.Length ? right.data[i] : 0U;\n                z[i] = xu &amp; yu;\n            }\n            return new FixedUIntXInt(z);\n        }\n        private class FixedUIntXIntConverter : 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 FixedUIntXInt(\"0\", 64);\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>Variable Bit Width Unsigned Integer 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":[],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/181"}],"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=181"}],"version-history":[{"count":5,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/181\/revisions"}],"predecessor-version":[{"id":442,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/181\/revisions\/442"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}