I'm looking for a quick way to generate every binary number of length n
which contains m
1s.
So for example, if n=3, m=2
, this would just be: 110, 011, 101
.
The naive approach would be to iterate through numbers between 1
and 2^n - 1
, checking if each value's binary representation contains m
1s. Alternatively, using some algorithm to get each combination of m
1s and n - m
0s.
However, I'm wondering if there might be some quicker way to do this with bitwise operators, or perhaps a property of numbers with m
1s in their binary representation that could be exploited.