/// <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); }