Moid Shaikh - 9 months ago 53

Java Question

This snippet takes inputs as boolean and returns output for ex-nor gate

`public boolean exnor_gate(boolean a, boolean b) {`

if (a==true){

if(b==true)

//a1, =b=1

return true;

else

//a=1, b=0

return false;

}

else {

if(b==true)

//a=0, b=1

return false;

else

//a=0, b=0

return true;

}

}

Is there any simple implementation for the above code?

Answer Source

You can use the `^`

operator, which is the XOR operator. You can implement it as such

```
public boolean exor_gate(boolean a, boolean b) {
return a ^ b;
}
```

Here is a truth table of the results

```
A | B | XOR
-----------
T | T | F
T | F | T
F | T | T
F | F | F
```

**Edit**: As Erwin noted, you might be trying to implement the XNOR which is `!(a ^ b)`

or `a == b`

. This would be implemented as such

```
public boolean xnor_gate(boolean a, boolean b) {
return a == b;
}
```

With truth table:

```
A | B | XNOR
------------
T | T | T
T | F | F
F | T | F
F | F | T
```