This ppt describes one of the interesting algorithms to count the number of bits set in an unsigned integer. x = (x & 0x55555555) + ((x>>1)&0x55555555)); x = (x&0x33333333) + ((x>>2)&0x33333333); ... ....