I do some research and I wrote some simple programs in java that suits my needs. I used logical operators AND,OR,XOR on integers but I miss XNOR operator. That what I'm looking for is a XNOR operator that behaves just same as others mentioned (could be applied on integers). Is it possible from somebody to write such a class in Java?
XNOR = inverse of XOR.
Bitwise XOR (exclusive or) "^" is an operator in Java that provides the answer '1' if both of the bits in its operands are different, if both of the bits are same then the XOR operator gives the result '0'. XOR is a binary operator that is evaluated from left to right.
XNOR gives the reverse of XOR if binary bit. First bit | Second bit | XNOR 0 0 1 0 1 0 1 0 0 1 1 1 It gives 1 if bits are same else if bits are different it gives 0. Recommended: Please try your approach on {IDE} first, before moving on to the solution.
boolean xnor(boolean a, boolean b) { return !(a ^ b); }
or even simpler:
boolean xnor(boolean a, boolean b) { return a == b; }
If you are actually talking about bitwise operators (you say you're operating on int
), then you will need a variant of the first snippet:
int xnor(int a, int b) { return ~(a ^ b); }
The logical XNOR operator is ==
.
Do you mean bitwise?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With