4 ms·
You weren't doing anything wrong. He discusses two methods: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan https://graphics.stanford
by tjalfi 10y ago
You weren't doing anything wrong.
He discusses two methods:
https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan https://graphics.stanford.edu/~seander/bithacks.html#CountBi...
https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel https://graphics.stanford.edu/~seander/bithacks.html#CountBi...
The second option will probably beat a byte lookup table. GCC switched from a lookup table to similar code. If this still isn't fast enough, try using SIMD registers and the Harley/Seal method based on carry save adders (http://www.hackersdelight.org/hdcodetxt/popArrayHS.c.txt http://www.hackersdelight.org/hdcodetxt/popArrayHS.c.txt). If you like this sort of bit twiddling, you probably would enjoy Henry Warren's Hacker's Delight.