4 ms·
This is useful to know for counting the bits set in a word.
by rahilb 8y ago
This is useful to know for counting the bits set in a word.
- CapacitorSet 8y agoWhich isn't super helpful, because there is [a GCC `__builtin_popcount`](https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html), a POPCNT intrinsic if you're developing for SSE4, and [existing implementations](https://en.wikipedia.org/wiki/Hamming_weight#Efficient_implementation https://en.wikipedia.org/wiki/Hamming_weight#Efficient_imple...) you can look up at any time if you don't want to reinvent the wheel.
- rahilb 8y agoThat's cool. I probably shouldn't have said useful and instead said interesting. My interest in these kind of operations is to learn lower level theory, not to produce production grade bit counting/twiddling code. In that sense, the operation is interesting (to me).
- hedora 8y agoThe “clear the bottom bit” hack is pretty cool. It’s sometimes faster to roll a loop that uses that than to invoke the GCC popcnt intrinsic. If you pass an -march parameter that lets GCC emit the intended intel assembly instruction, then the intrinsic seems to always win (as expected!)