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 the greatest page in the entire internet. If anyone can explain to me how computing the next bit permutation (the last version) works, please do.
1s has to stay the same. It's just the smallest number that is both greater than the current one and has the same number of ones. So we calculate the smallest possible increase while maintaining the number of ones as follows: 1. Take the rightmost maximal block of1s. Assume it has lengthnand there arem0s to its right. 2. Change the0to its left to a1. 3. Zero out the block of1s and 4. setn-1bits of lowest significance to1s. The algorithm, slightly modified using chars and more step-by-step, looks like this: Note that-~xis just a stupid way of writingx+1, as arithmetic negation in two's complement is equivalent to negating bit-wise and adding 1 after. Also,__builtin_ctz(cur_perm)equalsm. For example, saycur_perm = 0b10111000. 1. Stepasets the rightmost0s to1:a=10111111. 2. Stepbflips the0left of the rightmost block of1s and zeroes out everything to its right:b=11000000. 3. Stepcconsists (exactly) of all1s to the right of the flipped bit:c=00111111. That arem+n1s. 4. Stepdshiftscbym+1, therefore leaving then-1least significant bits as1s:d=00000011. 5. The final step puts together the more significant bits inband the bits of lower significance ind.