Java: How to output all possible binary combinations (256 different sequences)?
Solution 1
No need for the array. Here is a slight modification to your code that will output all the permutations.
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
for (int k = 0; k < 2; k++){
for (int l = 0; l < 2; l++){
for (int m = 0; m < 2; m++){
for (int n = 0; n < 2; n++){
for (int o = 0; o < 2; o++){
for (int p = 0; p < 2; p++){
System.out.println("" + i + j + k + l + m + n + o + p);
}
}
}
}
}
}
}
}
Do you have to use nested loops? Because this is trivially easy when you simply take advantage of the fact that the binary representation of all the numbers from 0 through 255 cover every permutation.
for (int i=0; i<256; i++) {
System.out.println(Integer.toBinaryString(i));
}
Solution 2
Just print the for
variables (i...p
) without accessing this obscure empty array.
Solution 3
Well, if it's OK to use some built-in classes, you can do this in the following way:
for (int i=0; i<256; i++){
System.out.println(Integer.toBinaryString(i));
}
And the second way (I believe you should use it, because looking by looking at it you can understand what's under the hood instead of "some magic", it uses bit mask
):
for (int i=0;i<256;i++){
int mask = 256;
while (mask > 0){
if ((mask & i) == 0){
System.out.print("0");
} else {
System.out.print("1");
}
mask = mask >> 1;
}
System.out.println();
}
user2411290
Updated on June 14, 2022Comments
-
user2411290 almost 2 years
I need to create a function that outputs all possible binary combinations (2^8 == 256 different sequences of 8 bits.). I'm really stumped on this. I have to do it with nested loops, and am not sure how to go about it. Below is what I tried so far. I was told that I could write this program using 8 nested loops, each one going from 0 to 1; Also, I could try to do this with bit manipulation operators.
Although what I have below is obviously wrong, I tried my best to show that I at least tried this. I also need to put new line's after each closing bracket, to separate the output.
The output should look like this:
00000000
00000001
00000010
00000011
00000100
...
11111110
11111111
public static void outputBinary(){ int[][][][][][][][] num = new int[2][2][2][2][2][2][2][2]; for (int i = 0; i < 2; i++){ for (int j = 0; j < 2; j++){ for (int k = 0; k < 2; k++){ for (int l = 0; l < 2; l++){ for (int m = 0; m < 2; m++){ for (int n = 0; n < 2; n++){ for (int o = 0; o < 2; o++){ for (int p = 0; p < 2; p++){ System.out.print(num[i][j][k][l][m][n][o][p]); } }}}}}}}
}
Thanks for looking.
-
user2411290 about 10 yearsMy instructions were: "You can use nested loops, if your familiar with how base 2 works, you can program it by extracting the base 2 digits; if you're familiar with bit manipulation operators, you could use them to output the values." I'm just trying to do it in a way that makes the most sense for me conceptually.
-
Marko Topolnik about 10 yearsFrom the deleted answer by Asaph:
System.out.println("" + i + j + k + l + m + n + o + p);
-
user2411290 about 10 yearsThank you very much. This is somewhat embarrassing that I don't know how to do this, but I will make sure to study/practice it until I do. Thanks for your help.
-
user2411290 about 10 yearsThanks for your help, nikis!
-
user2411290 about 10 yearsThanks for helping me out!
-
Marko Topolnik about 10 yearsBTW your one-liner won't be satisfactory because leading zeros are important to meet the requirement.
-
nikis about 10 years@user2411290 I've added the second way - it should be used in education purposes instead of "some magic", it uses bit manipulation
-
Asaph about 10 years@MarkoTopolnik To get the zero padding: stackoverflow.com/a/4421438/166339 . Accepted answer is kinda ugly but satisfies the requirement.