3 ms·
The decrement test for a power of two can be modified to count bits and runs in log n time. int bits(unsigned n) { int i = 0; while (n > 0) { n &
by ChristianMarks 15y ago
The decrement test for a power of two can be modified to count bits and runs in log n time.
int bits(unsigned n)
{
int i = 0;
while (n > 0) { n &= n-1; i++ }
return i;
}
- axylone 15y agoMuch better ways to count bits set: http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive http://graphics.stanford.edu/~seander/bithacks.html#CountBit...