Does Java have an XOR operator? If so, what is the representation for XOR in Java?
Yes, Java does have an XOR (Exclusive Or) operator – it is represented by the caret character – the “^”.
How does the XOR operator work in Java?
The XOR operator will return a 1, or TRUE, if both numbers being compared are different. But, if the numbers being compared are the same it will return a 0 for FALSE.
Here is a table that represents how the XOR operator works – where X and Y are the inputs and the output after the XOR operator is applied to those two numbers is shown in the third column:
An example of how to use the XOR operator in Java:
If you want to apply the XOR operator to two numbers, A and B, then this is what it would look like:
C = A ^ B; //find the XOR of A and B, store in C
XOR Java interview question
You can read another interesting example of using the XOR operator in the context of an interview question right here: XOR in Java interview question.