How would i go about finding the number of 'zero' bits in C++. Suppose I have an integer;
int value = 276;
For which I have the bits 100010100, but how do I count the zeros?
If you want efficiency then there is a good implementation in the book "Hackers Delight"
22 instructions branch free.
unsigned int count_1bits(unsigned int x)
{
x = x - ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = x + (x >> 8);
x = x + (x >> 16);
return x & 0x0000003F;
}
unsigned int count_0bits(unsigned int x)
{
return 32 - count_1bits(x);
}
I'll try to explain how it works. It is a divide-and-conquer algorithm.
(x >> 1) & 0x55555555
Shifts all bits 1 step to the right and takes the least significant bit of every bit pair.
0x55555555 -> 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 (16x2 bit pairs)
So basically you will have the following table of all 2 bit permutations.
1. (00 >> 1) & 01 = 00
2. (01 >> 1) & 01 = 00
3. (10 >> 1) & 01 = 01
4. (11 >> 1) & 01 = 01
x - ((x >> 1) & 0x55555555);
Then you subtract these from the non shifted pairs.
1. 00 - 00 = 00 => 0 x 1 bits
2. 01 - 00 = 01 => 1 x 1 bits
3. 10 - 01 = 01 => 1 x 1 bits
4. 11 - 01 = 10 => 2 x 1 bits
x = x - ((x >> 1) & 0x55555555);
So now we have changed every 2 bit pair so that their value is now the number of bits of their corresponding original 2 bit pairs... and then we continue in similar way with 4 bit groups, 8 bit groups, 16 bit groups and final 32 bit.
If you want a better explanation buy the book, there are a lot of good explanation and discussions of alternative algorithms etc...