In this tutorial we will learn what XOR operation in Java and how it works.

First, we learn the theoretical and logical understanding of XOR in Java and then after that, we will see how to use XOR in Java with an example.

**XOR Operator Introduction** in Java

XOR operator is a binary operator and it is denoted by “^”. XOR is a logical operation and also called exclusive or.

Operator XOR takes two boolean operands as input and returns true if and only if both the boolean operands are different. So, it is very clear that if both the boolean operands are the same then it returns the false results.

Below is the table in which we have two operands A and B and we apply **A XOR B** on them.

A | B | A^B |

true | true | false |

true | false | true |

false | false | false |

false | true | true |

It is very important to know that operator **“^”** is undefined for the argument type String. It is one of the important things which we have to keep in mind.

Also, **A XOR B **is equivalent to below logical operation:

`(A AND !B) OR (!A AND B)`

So, in the above-mentioned logical operation, it is important to know that **AND **operator has greater precedence than **OR**.

**Use XOR**

Now we will learn how to use XOR in Java. We see the logical operation which expresses the XOR of two operands. But logical operation with the help of OR and AND operation is a bit longer as compared to the normal “^” operator.

So, lets understand this scenario with the help of an example:

Suppose a class that has two boolean attributes: A and B. So now we want to tell if the result of A and B after applying logical XOR operator on them is either true or false.

If we use the logical operation **(A AND !B) OR (!A AND B) **for this then below is the logical operation equation for this:

```
//Suppose A =3 and B=5
(3 AND !5) OR (!3 AND 5)
```

It is a very long procedure especially when we have an alternative to use “^” directly.

So, we use the bitwise operator “^” directly and it returns a 1 if both the matching bits are different and returns false if both the matching bits are the same.

Below is the example with the bitwise operator:

`boolean result= A^B; // 3^5 `

So, we can see by using “^” our logical operation is a bit small in terms of length and also it is an efficient way.

If you know about the other bitwise operators, then surely you know that bitwise operators work with every primitive type.

Also, it is important to know that the **XOR **operator is also working with every **primitive **type.

Let’s take two integers 1 and 2. So 1 is represented as 0001 and 2 is represented as 0010.

`1 ^ 2 = 0011`

Which is 3.

**Java XOR Operator For Your Advantage Example**

One question is in everyone’s mind that how we use XOR operator in the real coding example.

One very basic and classic example of XOR operators is two swap two variables without using the temporary variable.

```
X = X ^ Y;
Y = X ^ Y;
X =X ^ Y;
```

So, when you analyze the above code snippet then you will be able to know that after the second line ( Y= X^Y) Y is equal to the original value of X.

And after 3^{rd} line X will be equal to the original value of Y.

So below is the complete code example of swap two numbers without using a temporary variable and by using XOR operator.

##### SwapTwoNumberUsingXor.java

```
package com.test;
import java.util.Scanner;
public class SwapTwoNumberUsingXor {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(" Enter First Integer A");
Integer A = scan.nextInt();
System.out.println("Enter Second Integer B");
Integer B = scan.nextInt();
System.out.println("Before Swapping");
System.out.println("Value of A is " + A);
System.out.println("Value of B is " + B);
A = A ^ B;
B = A ^ B;
A = A ^ B;
System.out.println("After Swapping");
System.out.println("Value of A is " + A);
System.out.println("Value of B is " + B);
scan.close();
}
}
```

#### Output

```
Enter First Integer A
38
Enter Second Integer B
29
Before Swapping
Value of A is 38
Value of B is 29
After Swapping
Value of A is 29
Value of B is 38
```

**Conclusion**

XOR operator is a bitwise operator that compares the bits one by one and returns 0 if the matching bits are the same and returns 1 if the matching bits are different (not the same).

XOR operator is very useful when you want to swap two numbers without using a temporary variable. Also, it is useful for checking the parity.

Hope you guys like the tutorial. Please comment if you find anything incorrect or need any help regarding this topic.

Happy Learning.

Pingback: 3 Different Ways to Swap Two Numbers in Java | Codez Up