I have created this code for both boolean and integer values to display a truth table for an "AND","OR","XOR", "NOT" gate. However I think that my code needs reviewing as it could be simplified.
public class LogicalOpTable {
public static void main(String[] args){
boolean p,q;
System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
p = false;
q = false;
System.out.print(p + "\t" + q + "\t" + (p&&q) + "\t");
System.out.println((p||q)+"\t"+(p^q)+"\t"+(!p));
p = false;
q = true;
System.out.print(p + "\t" + q + "\t" + (p&&q) + "\t");
System.out.println((p||q)+"\t"+(p^q)+"\t"+(!p));
p = true;
q = false;
System.out.print(p + "\t" + q + "\t" + (p&&q) + "\t");
System.out.println((p||q)+"\t"+(p^q)+"\t"+(!p));
p = true;
q = true;
System.out.print(p + "\t" + q + "\t" + (p&&q) + "\t");
System.out.println((p||q)+"\t"+(p^q)+"\t"+(!p));
System.out.println();
withBinary();
}
public static void withBinary(){
System.out.println("A\tB\tAND\tOR\tXOR\tNOT");
int a = 0;
int b = 0;
int and = a&b;
int or = a|b;
int xor = a^b;
int not = a;
if(a==0 && b == 0 )
not = 1;
System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));
b=1;
and = a&b;
or = a|b;
xor = a^b;
not = a;
if(a==0 && b == 1)
System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));
a=1;
b=0;
not = b;
and = a&b;
or = a|b;
xor = a^b;
not = a;
if(a==1 && b == 0)
System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));
a=1;
b=1;
not = 0;
and = a&b;
or = a|b;
xor = a^b;
if(a==1 && b == 1)
System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));
}
}
-
\$\begingroup\$ I don't really get the point of this program. Has it any purpose other than print some debug-values? \$\endgroup\$tkausl– tkausl2015年08月06日 11:35:58 +00:00Commented Aug 6, 2015 at 11:35
-
\$\begingroup\$ Its just a simple program to print out a truth table for the logic gates, once in boolean and then in integers. It was a question in a book. They already completed the boolean part, and then it says attempt to create a new logic table with the binary equivalent values. \$\endgroup\$mp252– mp2522015年08月06日 11:42:00 +00:00Commented Aug 6, 2015 at 11:42
1 Answer 1
First part
You want to iterate through all possible combinations of for a pair of booleans, so you can make the code reflect that explicitly and make it simpler:
System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
for (boolean p : new boolean[] {true, false}) {
for (boolean q : new boolean[] {true, false}) {
System.out.print(p + "\t" + q + "\t" + (p&&q) + "\t");
System.out.println((p||q)+"\t"+(p^q)+"\t"+(!p));
}
}
System.out.println();
Second part
You have something suspicious in your if
statements. As you don't use braces, this code smells of a copy&paste bug:
if(a==0 && b == 0 )
not = 1;
System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));
If the condition is met, it will execute the line not = 1;
. The second line will be executed independently of the condition. I strongly suggest you use braces even for 1-line blocks, like this:
if (a == 0 && b == 0) {
not = 1;
System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));
}
This way you can avoid this kind of bugs.
Now, applying the same reasoning as with the previous method, you can rewrite it as:
System.out.println("A\tB\tAND\tOR\tXOR\tNOT");
for (int a : new int[] {0, 1}) {
for (int b : new int[] {0, 1} ) {
System.out.println(a + "\t" + b + "\t" + (a & b) + "\t" + (a | b) + "\t" + (a ^ b) + "\t" + ~a);
}
}
BUT What do you want to accomplish with the NOT operation? Do you mean the Bitwise Complement? I used that, but maybe you want a function that returns 0 when it's 1, and 1 when it's 0. In that case, you need to replace the ~a
with something like (a == 0) ? 1 : 0
.
So the whole code could be reduced to just:
public static void printTable() {
System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
for (boolean p : new boolean[]{true, false}) {
for (boolean q : new boolean[]{true, false}) {
System.out.print(p + "\t" + q + "\t" + (p&&q) + "\t");
System.out.println((p||q)+"\t"+(p^q)+"\t"+(!p));
}
}
System.out.println();
System.out.println("A\tB\tAND\tOR\tXOR\tNOT");
for (int a : new int[]{ 0, 1}) {
for (int b : new int[]{0, 1} ) {
System.out.println(a + "\t" + b + "\t" + (a & b) + "\t" + (a | b) + "\t" + (a ^ b) + "\t" + ~a);
}
}
}
-
\$\begingroup\$ You could even wrap your formatting "print" code in named method for enhanced readability. \$\endgroup\$Diego Martinoia– Diego Martinoia2015年08月06日 14:05:10 +00:00Commented Aug 6, 2015 at 14:05