In this short tutorial we’ll examine the Java XOR Operator and look at how it can be used.

The **XOR operator in Java** can be considered as a logical operator. It is also known as the exclusive or, as it takes two operands, and only returns true if both operands have a different value.

For example:

1 2 3 4 5 | ```
A B A XOR B
true true false
true false true
false true true
false false false
``` |

Above, we can see the results of applying the XOR operator to different boolean values. The table shows that if one value is true and the other is false, the result is true, otherwise the result of the XOR operator is false.

The **XOR** operation can be applied in Java in different ways. For example, we can simulate the effect of an XOR operator by using Java logical operators.

Consider the contrived example below. In which a thread can be running or blocked.

1 2 3 4 | ```
boolean blocked = false;
boolean running = true;
boolean result = (running && !blocked) || (!running && blocked);
``` |

Above we simulate the XOR operation by using a combination of Java’s logical operators and(&&), or(||) and not(!), ensuring that both variables `blocked`

and `running`

have different boolean values.

However, writing code like above, requires to much work, and requires unnecessary mental effort to understand. We could solve the above shortcomings by creating a reusable utility method.

1 2 3 | ```
public static boolean xor(final boolean left, final boolean right) {
return (left && !right) || (!left && right);
}
``` |

Above we create a utility method `xor`

to simulate the XOR operation. We can now perform the previous operation using this method.

1 | ```
XOROperatorExample.xor(running,blocked);
``` |

However, with a bit of investigation we find that Java provides this operator out of the box. Moreover, it works with other primitive types, and not just the boolean type that we used above.

The XOR operation is realized in Java with the ^ symbol. Let’s write a test to verify that this operator works as expected.

1 2 3 4 5 6 7 | ```
@Test
public void xor() {
assertFalse(true ^ true);
assertFalse(false ^ false);
assertTrue(true ^ false);
assertTrue(false ^ true);
}
``` |

Now let’s re-factor our first example to use the Java XOR operator.

1 | ```
boolean result = running ^ blocked;
``` |

As we can see the code is more concise. The XOR operator in Java is a bitwise operator. The operator works with Java primitive types and just like other bitwise operators, it compares the bits of its operands one at a time.

For example consider we apply the XOR operator to the integer values: 2, and 2.

1 2 3 4 5 6 7 | ```
@Test
public void bitwiseXOR() {
int a = 2; //0010
int b = 2; //0010
int result = a ^ b;
assertEquals(0, result);
}
``` |

1 2 3 4 | ```
0010
+ 0010
---------
0000
``` |

As both integer values have the same bit pattern `0010`

applying the XOR operator turns all the bits off resulting in the pattern `0000`

which is equivalent to the integer value 0.

In this tutorial we learned about the XOR operator. We examined how this could be implemented in Java. We also briefly touched on the binary arithmetic performed by this operator.