I have a process that returns bit strings of given length N
and given popcount k
, i.e., each bitstring has exactly k
ones and N - k
zeros.
I need to counts how often each bit string is returned. Currently, I use the associated base2-integer to increment a counter in a 2^N
-dimensional array. However, this becomes impractical for large N
, since the array is too large. To do better, I would like to exploit the fact that I know the popcount k
beforehand, so I would only need an array of dimensions M = N choose k
to store the counts. To do this I need a procedure to turn a given bitstring to a unique integer, e.g. for the example N=3
, k=2
, the following mapping would suffice:
011 → 1
110 → 2
101 → 3
Note that I don't require the reverse mapping, although it should exist. What could be such a mapping? Is this (related to) a known problem?