3 ms·
Wow! Just 5 instructions. This is almost 4 times faster than my previous best solution: // 19 instructions, does not use intrinsics int countbits(unsigned
by winstonprivacy 4y ago
Wow! Just 5 instructions. This is almost 4 times faster than my previous best solution:
// 19 instructions, does not use intrinsics
int countbits(unsigned x) {
unsigned n;
n = (x >> 1) & 0x77777777;
x = x - n;
n = (n >> 1) & 0x77777777;
x = x - n;
n = (n >> 1) & 0x77777777;
x = x - n;
x = (x + (x >> 4)) & 0x0F0F0F0F;
x = x\*0x01010101;
return x >> 24;
}
Amazing tip, thanks.