patternjavaMinor
Logical gates with integer values
Viewed 0 times
withgatesvalueslogicalinteger
Problem
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 &&
```
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 &&
Solution
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:
Second part
You have something suspicious in your
If the condition is met, it will execute the line
This way you can avoid this kind of bugs.
Now, applying the same reasoning as with the previous method, you can rewrite it as:
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
So the whole code could be reduced to just:
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);
}
}
}Code Snippets
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();if(a==0 && b == 0 )
not = 1;
System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));if (a == 0 && b == 0) {
not = 1;
System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));
}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);
}
}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);
}
}
}Context
StackExchange Code Review Q#99185, answer score: 8
Revisions (0)
No revisions yet.