Enumerate all possible binary numbers with fixed amount of 1 in ascending order

68 Views Asked by At

I'm quite stuck on finding a formula for computing the ordinal number for a binary number with fixed amount of 1 and fixed size (i.e. digits in total).

Let's say I'm looking at all binary numbers with 22 digits and having exactly 10 ones, i.e.:

  • $0000000000001111111111_2$ -> first index
  • $0000000000010111111111_2$ -> second index
  • $0000000000011011111111_2$ -> third index
  • ...
  • $0000001011111001101100_2$ -> ...
  • ...
  • $1111111111000000000000_2$ -> 646646th index

Note that these numbers should be ordered ascending. I can easily generate all these numbers with Java code and know therefore it should be 646646 numbers in total for this configuration of digits (10 ones and 12 zeros).

Given any of these 646646 binary numbers, how could I compute the index $n$ where $1 \le n \le 646646$.

Edit: or maybe there is already an algorithm for that I just don't the name, or the name of this particular problem.