HOWTO · Java
The XOR Operator in Java
Use Java's ^ operator for Boolean and integral XOR, with tested examples and common pitfalls.
On this page
In Java, ^ is the XOR (exclusive OR) operator. Use it with boolean values when exactly one condition must be true, or with integral values when you need a bitwise XOR. It is not an exponentiation operator: Java has no ** operator. The Java Language Specification defines both forms of ^.
Choose the Boolean form for conditions and the integer form for bits or masks. The operator is available in current Java releases; this article’s examples were executed with OpenJDK 17.0.20.1, while the preparation research used the current JDK 26 release information.
Use XOR With Boolean Values
For two Boolean operands, XOR is true only when the operands differ. Its truth table is simple:
| Left | Right | left ^ right |
|---|---|---|
false |
false |
false |
false |
true |
true |
true |
false |
true |
true |
true |
false |
Unlike && and ||, ^ evaluates both operands. Do not use it as a replacement for a short-circuit guard such as value != null && value.isReady(): the method call is still evaluated. Use it when evaluating both Boolean expressions is safe and “exactly one” is the rule.
This tested example shows Boolean XOR and an integer XOR together:
public class BooleanAndIntegerXor {
public static void main(String[] args) {
System.out.println(true ^ false);
System.out.println(true ^ true);
System.out.println(0b1100 ^ 0b1010);
}
}
Output:
true
false
6
The first two lines show the Boolean rule. For integers, 0b1100 ^ 0b1010 produces 0b0110, which is decimal 6: each result bit is 1 only where the input bits differ.
Use XOR With Integral Values and Masks
The bitwise form accepts integral primitive types such as byte, short, char, int, and long. Java applies binary numeric promotion before the operation, so expressions involving smaller integral types commonly have an int result. float and double cannot be XOR operands. Cast only when the destination type really requires it; a cast can discard bits.
XOR is useful for toggling selected bits. If flags is an integer and mask identifies the bits to change, flags ^ mask flips exactly those bits. Applying the same mask again restores the original bits because x ^ y ^ y equals x. That reversible property is useful for controlled bit manipulation, but it does not make XOR a general substitute for clearer application-level logic.
Find One Value That Does Not Have a Pair
A compact use of integer XOR finds one value when every other value occurs exactly twice. Equal values cancel because x ^ x is 0, and 0 ^ x is x.
public class UniqueNumberXor {
public static void main(String[] args) {
int unique = 0;
for (int value : new int[] {4, 9, 4}) {
unique ^= value;
}
System.out.println(unique);
}
}
Output:
9
This is a boundary-sensitive technique, not a general “find a unique number” algorithm. It works only when one value is unpaired and every other value appears exactly twice. For example, it does not identify a single unique value if another value appears three times. Choose a counting approach when the occurrence rule is different or when you need frequencies as well as the unique value.
Prefer Clear Alternatives Where They Fit
Use != when comparing two Boolean values for inequality; it can communicate that intent more directly than ^. Use & 1 to test whether an integer is odd or even—the parity operation is AND, not XOR. Although XOR swapping two variables is a well-known trick, a temporary variable or normal assignment is clearer in Java and avoids aliasing mistakes.
The key distinction is therefore straightforward: ^ means exclusive OR for boolean values and bitwise XOR for integral values. Check operand types, remember that both Boolean sides are evaluated, and state the data assumptions before relying on duplicate cancellation.