CKTIS / Java
Bitwise XOR operator (^)
The ^ operator applies XOR to corresponding bits of integral values. For boolean values, it tests whether they differ.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
left ^ rightThe ^ operator sets a result bit to 1 when corresponding bits differ. Thus, 0b1100 ^ 0b1010 produces 0b0110, or 6. It does not mean exponentiation.
Integral operands undergo binary numeric promotion. If either operand is long, the other operand is promoted directly to long if needed, without first converting to int, and the result is long. Otherwise, any byte, short, or char operand is promoted to int, and the result is int. Types float and double are not allowed.
For boolean or Boolean, the result is boolean: true exactly when the values differ. The operator does not short-circuit. Unboxing null throws NullPointerException.
An XOR mask toggles selected bits; applying the same mask again restores the value. Operator precedence decreases in the order &, ^, |; use parentheses for clarity.
Examples
public class Main {
public static void main(String[] args) {
int value = 0b1100;
int mask = 0b1010;
int result = value ^ mask;
System.out.println(result);
System.out.println(result ^ mask);
System.out.println(true ^ false);
System.out.println(true ^ true);
}
}Result
6 12 true false
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.