I need to generate such a 32-bit binary sequence: it has eight ‘1’ and the rest bits is ‘0’. So written in hex, some of the sequences will be like these:
000000FF
000001FE
000001FD
000001FB
000001F7
...
The problem is to choose 8 from 32, so there are 10518300 combinations. In other words, there are 10518300 sequences like my examples. I will appreciate it if you give me any suggestions or algorithm to generate my desired sequences.
This problem is about making permutations of zeros and ones. The easiest to code solution is to use
next_permutationand avector<bool>.Prepare a vector with the earliest permutation in lexicographic order (ones are at the back). Run
next_permutationuntil it returnsfalse. Here is a demo code that generates all 8-bit sequences with three bits set:Here is a link to ideone with a running demo.
Your program will need a 32-bit vector with the last eight elements set to
1. Instead of printing the elements of the sequence, you would need to convert them to a 32-bitint, and store them in the output container.