Binary Exclusive OR Assign Operator

XOR or the Binary Exclusive OR operation is a common bitwise operation in any language, including Java. Below are examples on how to use Binary Exclusive OR Assign Operator in the Java programming language:

Binary Exclusive OR Assign With Literal

We use the symbol ^= to denoted binary exclusive OR assign operation. This operation will perform an XOR on the variable with the given expression, and assign back to the variable. See below example:
int a = 140;
a ^= 50;
System.out.println(a);
The XOR operation on 140 and 50 gives the result of 190, which is assigned back to variable a, yielding below output:
190

Binary Exclusive OR Assign With Another variable

Below is a simple example on how to perform Binary Exclusive OR Assign With Another variable in Java.

int a = 95;
int b = 11;
a ^= b;
System.out.println(a);

This is doing XOR on the two variables, and assigning the result back to variable a, which is:
84

Binary Exclusive OR Assign With An Expression

Below is using a more complex expression rather than simple literal value or variable, for our XOR operation.
int a = 15;
int b = 7;
a ^= b * 7;
System.out.println(a);
So we get the product of 7 times 7, which is 49, and do an XOR with 15. This gives us:
62