countOneBits

Count 1 bits in x reperesented as binary.

Parameters: n_bits = The size of the counted range in bits. The maximum value is 8.

uint
countOneBits
(
int x
,
uint n_bits
)

Examples

1 uint[] bits = [0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1];
2 SuccinctBitVector bitvector = new SuccinctBitVector();
3 foreach(uint bit; bits) {
4     bitvector.append(bit);
5 }
6 
7 ///bitvector.bitarray[0] = [0, 1, 1, 0, 1, 1, 0, 0]
8 //countOneBits(bitvector.bitarray[0], 2) should return the number of 1 bits
9 //in [0, 1]
10 assert(countOneBits(bitvector.bitarray[0], 2) == 1);
11 
12 //countOneBits(bitvector.bitarray[0], 3) should return the number of 1 bits
13 //in [0, 1, 1]
14 assert(countOneBits(bitvector.bitarray[0], 3) == 2);
15 
16 ///bitvector.bitarray[1] = [1, 0, 0, 0, 0, 0, 0, 1]
17 //countOneBits(bitvector.bitarray[1], 2) should return the number of 1 bits
18 //in [1, 0]
19 assert(countOneBits(bitvector.bitarray[1], 2) == 1);
20 //countOneBits(bitvector.bitarray[1], 8) should return the number of 1 bits
21 //in [1, 0, 0, 0, 0, 0, 0, 1]
22 assert(countOneBits(bitvector.bitarray[1], 8) == 2);

Meta