int NumberOfSetBits32(int i)
{
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
i = ((i + (i >> 4)) & 0x0F0F0F0F);
return (i*(0x01010101))>>24;
}
unable to understand the above code snipplet.Any one please help.
The above code uses bitwise properties to count 1s in the number fast. 0x represents hex numbers, 0x5555555 is a sequence of bits 0101010101… similarly 0x3333 represents 0011001100110011 and so on.
Regarding the logic behind this approach check this Link .
It explains the different techniques to count set bits along with the above mentioned algo and their respective efficiency.