The first line treats the integer as an array of 16 2-bit integers, the result of the expression is 0 if 0 bits in the 2-bit pair were set, 1 if 1 bit in the 2-bit pair was set (01 bin or 10 bin), and 2 if both bits of the 2 bit pair were set. This is because we consider the lower of every two bits of x
, and the lower of every two bits of x
shifted right by one; adding them together. We know no carries will occur outside of the 2-bit pairs because our summands are limited to 0 or 1. The result is then stored back into x
.
x = (x & (0x55555555)) + ((x >> 1) & (0x55555555));
The next line does the same thing, this time treating every 2-bits as a separate integer, storing their sum in every 4-bits the two summands used to occupy. After this operation the integer essentially becomes an array of 8 4-bit integers. Before the operation each summand is either 0, 1, or 2 (decimal) because it corresponds to the counts from the last line. Because of this we know each sum will be no greater than 4. Since each 4-bit int has a maximum value of 15 (decimal), we know this will also not overflow. As above, the result is stored back into x
.
x = (x & (0x33333333)) + ((x >> 2) & (0x33333333));
We do the same as above, this time suming every pair of 4-bits into every set of 8 bits.
x = (x & (0x0f0f0f0f)) + ((x >> 4) & (0x0f0f0f0f));
We then sum ever pair of 8-bits into every pair of 16 bits (the upper and lower half of x
).
x = (x & (0x00ff00ff)) + ((x >> 8) & (0x00ff00ff));
We now sum the upper and lower half of x
, and we are left with a 32-bit value equal to the number of bits set in the value of x
.
x = (x & (0x0000ffff)) + ((x >> 16) & (0x0000ffff));
The key here is that each step does an inplace pairwise count of bits until we are left with the total bit count in the 32-bit integer it self.