What is the fastest way to count set bits in UInt32
The bit-twiddling hacks page has a number of options. Of course, you could argue that iterating over all 32 possible bits is O(N) in that it’s the same cost every time 🙂 For simplicity, I’d consider the lookup-table-per-byte approach, or Brian Kernighan’s neat idea which iterates as many times as there are bits set, which … Read more