I recently implemented an algorithm that has to perform checks on all subsets of some large set. A subset of an $n$-sized set can be understood as a binary string of length $n$ where bit $i$ is set if and only if the $i$-th element is in the subset. During my search for code to enumerate such bitstrings, I found <a href="http://graphics.stanford.edu/~seander/bithacks.html" target="_blank">the greatest page in the entire internet</a>. If anyone can explain to me how <a href="http://graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation" target="_blank">computing the next bit permutation</a> (the last version) works, please do.
5 Replies to “Bit Twiddling Hacks”