/// <summary>
        /// Returns the sub bit array from the given <paramref name="bitArray"/> as an <see langword=""="UInt64"/>
        /// value.  The sub bit array, left or right, is selected by the given <paramref name="getFromLeftBits"/>
        /// boolean.
        /// </summary>
        public static ulong ToUInt64(IReadOnlyDuoBitArray bitArray, bool getFromLeftBits)
        {
            var byteArray = getFromLeftBits
                ? bitArray
                            .GetLeftBits(0, 8 * sizeof(ulong))
                            .ToByteArray()
                : bitArray
                            .GetRightBits(0, 8 * sizeof(ulong))
                            .ToByteArray();

            return(BitConverter.ToUInt64(byteArray, 0));
        }
        /// <summary>
        /// An O(n) method to calculate the Hamming weight of a bit array, where n is the total number of bits in
        /// the array.
        /// </summary>
        public int CountSetBits(IReadOnlyDuoBitArray readOnlyBitMap)
        {
            var byteArray  = readOnlyBitMap.ToByteArray();
            var numOneBits = 0;

            for (var i = 0; i < byteArray.Length; i++)
            {
                var thisByte = byteArray[i];
                while (thisByte != 0)
                {
                    numOneBits += (int)(thisByte % 2);
                    thisByte  >>= 1;
                }
            }

            return(numOneBits);
        }
Ejemplo n.º 3
0
 public void ShouldConvertBitArrayToUInt64(IReadOnlyDuoBitArray bitArray, bool getFromLeftBits)
 {
 }
 public int CountSetBits(IReadOnlyDuoBitArray bitMap) => bitCounter.CountSetBits(bitMap);