BitSharp.Core.DataCalculator.ToCompact C# (CSharp) Method

ToCompact() public static method

public static ToCompact ( UInt256 value, bool negative = false ) : uint
value UInt256
negative bool
return uint
        public static uint ToCompact(UInt256 value, bool negative = false)
        {
            var size = (HighBit(value) + 7) / 8;
            var compact = 0U;

            if (size <= 3)
            {
                compact = (uint)(value.Part4 << 8 * (3 - size));
            }
            else
            {
                value >>= 8 * (size - 3);
                compact = (uint)(value.Part4);
            }

            // The 0x00800000 bit denotes the sign.
            // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
            if ((compact & 0x00800000) != 0)
            {
                compact >>= 8;
                size++;
            }

            Debug.Assert((compact & ~0x007fffff) == 0);
            Debug.Assert(size < 256);

            compact |= (uint)(size << 24);
            if (negative && (compact & 0x007fffff) != 0)
                compact |= 0x00800000;

            return compact;
        }