Generating Combinations

Generating Combinations

On the Wikipedia page for combinatorial number systems I stumbled across Gosper’s hack which is a great little bit of code for generating all possible combinations of a bitset very quickly. The initial value should be the smallest number with K-bits set, which will just have all the lowest bits set. It returns the next combination with K of N bits set.

Source: andy.kitchen