Exemplo n.º 1
0
        /// <summary>
        /// Returns all bits in range as ulong.
        /// </summary>
        /// <param name="pos">Position in bit array.</param>
        /// <param name="numBits">Number of bits to get (must be 1-64).</param>
        /// <returns>Returns requested range of bits.</returns>
        public ulong GetBits(int pos, int numBits = 1)
        {
            CheckArgs(pos, numBits);

            var idxB   = pos >> 6;
            var shiftB = pos & 0x3f;

            if (shiftB + numBits < 64)
            {
                var mask = 0xfffffffffffffffful >> (64 - numBits);
                return(Bitwise.ExtractBits(Ptr[idxB], shiftB, mask));
            }

            var end    = math.min(pos + numBits, Length);
            var idxE   = end >> 6;
            var shiftE = end & 0x3f;

            var   maskB  = 0xfffffffffffffffful >> shiftB;
            ulong valueB = Bitwise.ExtractBits(Ptr[idxB], shiftB, maskB);

            var   maskE  = 0xfffffffffffffffful >> (64 - shiftE);
            ulong valueE = Bitwise.ExtractBits(Ptr[idxE], 0, maskE);

            return((valueE << (64 - shiftB)) | valueB);
        }