I am trying to generate all of the possible binary combinations for two bytes ex.
00000000 00000001 00000000 00000010 00000000 00000011
I have the class that I'm working on but its clearly not working at all. I cant get the method to return the output as it is generated.
I got the code below to work properly, but only for calculation 1 byte. How would I change this to calculate all of the possible outcomes for 2 bytes?
package referenceCode;public class BinaryGenerator {
private int val = 0; private int[] values = new int[]{0,1}; //This method converts the Binary Pattern output into a char[] so that it can be printed out to a file public int[] binaryPatternToString() { int numBits = 8; values[0] = 0; values[1] = 1; int[] returned = null; for (int i = 1; i < numBits; i++) { returned = binaryGenerator(i); for (int j = 1; j < numBits; j++) { } } return returned; } private int[] binaryGenerator(int iVal) { int[] moreValues = new int[values.length * 2]; int start = (int)Math.pow(2, iVal); for (int j = 0; j < values.length; j++) { moreValues[j * 2] = values[j] << 1; moreValues[j * 2 + 1] = values[j] << 1 | 1; } values = moreValues; for (int value : values) { System.out.println(Integer.toBinaryString(value)); } return moreValues; }}
Would it be a better idea or more efficient to make it a recursive method instead of a method with a for loop?