Importance of XOR operator in Java?



Bitwise XOR (exclusive or) "^" is an operator in Java that provides the answer '1' if both of the bits in its operands are different; if both of the bits are the same, then the XOR operator gives the result '0'. XOR is a binary operator that is evaluated from left to right. The operator "^" is undefined for the argument of type String.

Importance of the XOR (^) Operator in Java

In Java, the XOR (^) operator is important for both bitwise and boolean operations. It returns true or 1 only when the two operands "differ". XOR is commonly used for comparing bits, toggling values, and swapping variables without a temporary variable.

The following table shows how the XOR (^) operator works when applied to two binary digits:

Input 1 Input 2 Output
0 0 0
0 1 1
1 0 1
1 1 0

The above table helps understand the result when performing the XOR operation between two operands.

Example 1

The following example demonstrates the XOR (^) operator with Boolean values. XOR returns true only when the inputs differ. The program tests all combinations of true and false:

public class XORTest1 {
   public static void main(String[] args) {
      boolean x = false;
      boolean y = false;
      boolean xXorY = x ^ y;
      System.out.println("false XOR false: "+xXorY);
      x = false;
      y = true;
      xXorY = x ^ y;
      System.out.println("false XOR true: "+xXorY);
      x = true;
      y = false;
      xXorY = x ^ y;
      System.out.println("true XOR false: "+xXorY);
      x = true;
      y = true;
      xXorY = x ^ y;
      System.out.println("true XOR true: "+xXorY);
   }
}

The above program displays true or false based on whether the input combinations are different or the same.

false XOR false: false
false XOR true: true
true XOR false: true
true XOR true: false

Example 2

The following is another example of showing the importance of the XOR (^) operator. It performs the "XOR" operation on each character of str1 and str2 and appends the result (0 or 1) to a StringBuffer:

public class XORTest2 {
   public static void main(String[] args) {
      String str1 = "1010100101";
      String str2 = "1110000101";
         System.out.println("The str1 is: " + str1);
         System.out.println("The str2 is: " + str2);
      StringBuffer sb = new StringBuffer();
      for (int i = 0; i < str1.length(); i++) {
         sb.append(str1.charAt(i)^str2.charAt(i));
      }
      System.out.println("The final result is: " + sb);
   }
}

Output

The above program produces the following output ?

The str1 is: 1010100101
The str2 is: 1110000101
The final result is: 0100100000

Example 3

In the example below, we use the XOR (^) operator to swap two numbers without using a third variable. The XOR operator performs its operation on both variables and swaps their values:

public class Demo {
   public static void main(String[] args) {
      int a = 5;
      int b = 7;
      
      System.out.println("Before swapping: a = " + a + ", b = " + b);
      
      // Swap using XOR
      a = a ^ b;
      b = a ^ b; 
      a = a ^ b;
      
      System.out.println("After swapping: a = " + a + ", b = " + b);
   }
}

Output

The above program swapped the values of two numbers and displayed both the original values and the values after swapping:

Before swapping: a = 5, b = 7
After swapping: a = 7, b = 5

Example 4

The following example shows the best usage of the XOR (^) operator in Java. We use this operator to find a unique element from the given array {2, 3, 5, 4, 5, 3, 4}:

public class Demo {
   public static void main(String[] args) {
      int[] arr = {2, 3, 5, 4, 5, 3, 4};
      System.out.print("The given array is: ");
      for(int i = 0; i<arr.length; i++){
         System.out.print(arr[i] + " ");
      }
      int unique = 0;
      for (int num : arr) {
         unique ^= num;
      }
      System.out.println("\nThe unique element is: " + unique);
   }
}

Output

The above program displays the unique element in an array:

The given array is: 2 3 5 4 5 3 4 
The unique element is: 2
Updated on: 2025-05-14T19:35:13+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements