Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java XNOR operator

Tags:

java

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?

like image 944
user1092472 Avatar asked Dec 11 '11 17:12

user1092472


People also ask

What is XNOR in Java?

XNOR = inverse of XOR.

What is XOR operator in Java?

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.

How do you take XNOR?

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.


2 Answers

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); } 
like image 200
Oliver Charlesworth Avatar answered Sep 23 '22 03:09

Oliver Charlesworth


The logical XNOR operator is ==.

Do you mean bitwise?

like image 40
SLaks Avatar answered Sep 26 '22 03:09

SLaks