An algorithm for creating all possible combinations 0s & 1s for any length of digits

I would like to know how I can print n combinations of 1 and 0. The number of combinations, n is determined by the user. Expected results:

n is 1;

 0,1 

n = 2;

 00,01,10,11 

n = 3;

 000,001,010,011,100,101,110,111 

etc .. etc ..

The outputs will have 2^n number of combinations (where n is the number of expected digits in one combination).

How can I do this without using any inline function? The question does not depend on the language and is intended for the algorithm.

+4
source share
4 answers

You can simply list all numbers up to 2^n - 1 in binary format. This will leave you with the same combination.

list n = 2 to 2^3 - 1 = 7 Convert to binary:

 000 --> 0 001 --> 1 010 --> 2 011 --> 3 100 --> 4 101 --> 5 110 --> 6 111 --> 7 

EDIT . Fixed number of digits. It works

 #include <stdio.h> #define LENGTH 3 void print_binary(int n) { int bit = 1<<LENGTH - 1; while ( bit ) { printf("%d", n & bit ? 1 : 0); bit >>= 1; } printf("\n"); } int main(){ int n = 1<<LENGTH, i; for(i=0;i<n;i++) print_binary(i); } 
+11
source

If you don't care about speed and memory, you recursively use cold, which leads to a small and short solution:

 public static void print01PermutationsUpToLength(final String currentString, final int upTo) { if (upTo == 0) { System.out.println(currentString); return; } print01PermutationsUpToLength(currentString + "0", upTo - 1); print01PermutationsUpToLength(currentString + "1", upTo - 1); } 

(java. Obviously, this can be done in every language that allows recursion and call by value or a copy of String)

If you don't like the String argument, you can add a launch function:

 public static void print01PermutationsUpToLength(final int upTo) { print01PermutationsUpToLength("", upTo); } 

results:

 final int upToLength = 3; print01PermutationsUpToLength(upToLength); 000 001 010 011 100 101 110 111 

The formatting can be changed the way you want, it was just to better see the results.
You can change the order if you switch parts of the string structure ( currentString + "0" ).

+3
source
 void print_digit(int n,int digits) { int i; for(i=0;i<digits;i++) { if(n&(1<<(digits-i-1))) { putchar('1'); } else { putchar('0'); } } } print all_digits(int e) { for(i=0;i<(1<<e);i++) { print_digit(i,e); putchar('\n'); } fflush(stdout); } 
+2
source

This is my view on this issue. Created an array of characters, and you want to find a combination of k using the entire array. To solve this issue, our array contains: ['0','1'] .

Let's say we have a char set[] = new {'0','1'};

The method below will give you any number of combinations of zero and one. I tested it with a combination of 0 and 1 with a 50 character dataset.

 public void printLengthRec(char[] inputSet, String prefix, int k) { int sizeOfInputArray=inputSet.length; //TerminationCase: k is 0, print prefix if (k == 0) { System.out.println(prefix); return; } // One by one add all characters from set and recursively // call for k equals to k-1 for (int i = 0; i < 2; ++i) { // Next character of input added String newPrefix = prefix + set[i]; printLengthRec(inputSet, newPrefix, k - 1); } } 
0
source

All Articles