Java – truth table array
I always insist on how to start coding
The first case will be: t T. after I have completed other calculations on this result, I want to move forward and make case: T F and continue to run other calculations Can someone guide me in the right direction? I would appreciate it Algorithms in any language suit me Thank you for your time! (:
Solution
There are many ways to store binary data in Java. It is unclear what you want to store If you want to store all possible combinations of N flips, you need a new Boolean [2 ^ n] [n] with the array
Remember, Java has another power - boosting syntax
UPDATE
The following is the code that stores all combinations of N flip
From this you will learn how to generate a combination: from the binary representation of the combination sequence number Read the comments
// number of flips
// limit it by 31
int N = 3;
// number of combinations
// using bitshift to power 2
int NN = 1<<N;
// array to store combinations
boolean flips[][] = new boolean[NN][N];
// generating an array
// enumerating combinations
for(int nn=0; nn<NN; ++nn) {
// enumerating flips
for( int n=0; n<N; ++n) {
// using the fact that binary nn number representation
// is what we need
// using bitwise functions to get appropriate bit
// and converting it to boolean with ==
flips[nn][N-n-1] = (((nn>>n) & 1)==1);
// this is simpler bu reversed
//flips[nn][n] = (((nn>>n) & 1)==1);
}
}
// printing an array
for(int nn=0; nn<NN; ++nn) {
System.out.print("" + nn + ": ");
for( int n=0; n<N; ++n) {
System.out.print(flips[nn][n]?"T ":"F ");
}
System.out.println("");
}
