4 ms·
The best solution I know of is: a, i = 11, 0 while a > 0: a, i = a & a-1, i + 1 If there are k bits set out of n bits, it only does k iterations.
by sgk284 17y ago
The best solution I know of is:
a, i = 11, 0
while a > 0:
a, i = a & a-1, i + 1
If there are k bits set out of n bits, it only does k iterations.
- ralph 17y agoThat's http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan http://graphics.stanford.edu/~seander/bithacks.html#CountBit...