Java Program to Swap Two Numbers Using XOR Operator

In this tutorial, you will learn how to write a simple Java program to swap two numbers using the bitwise XOR (^) operator.

Problem Statement

Write a Java program to swap the values of two integer variables without using a third (temporary) variable. The program must use the bitwise XOR (^) operator to perform the swap.

For example, if:

firstNumber = 10
secondNumber = 20

After swapping:

firstNumber = 20
secondNumber = 10

Expected Solution Approach of the Program

We can swap two integer values using the bitwise XOR (^) operator in three steps without needing a temporary variable. Suppose we have initial values:

firstNumber = 10
secondNumber = 20

Step 1: XOR Both Numbers

First, XOR firstNumber with secondNumber and store the result in firstNumber.

firstNumber = firstNumber ^ secondNumber;

Conceptually:

firstNumber = 10 ^ 20
secondNumber = 20

Now, firstNumber contains the XOR result of the original two numbers.

Step 2: Recover the Original First Number

Next, XOR the updated firstNumber with secondNumber.

secondNumber = firstNumber ^ secondNumber;

Conceptually:

secondNumber = (10 ^ 20) ^ 20

Using the XOR property:

a ^ b ^ b = a

We get:

secondNumber = 10

So, secondNumber now contains the original value of firstNumber. The values of both variables are now:

firstNumber = 10 ^ 20
secondNumber = 10

Step 3: Recover the Original Second Number

Finally, XOR the updated firstNumber with the newly updated secondNumber.

firstNumber = firstNumber ^ secondNumber;

Conceptually:

firstNumber = (10 ^ 20) ^ 10

Using the XOR property:

a ^ b ^ a = b

We get:

firstNumber = 20

The values of both variables are now:

firstNumber = 20
secondNumber = 10

Therefore, the two numbers have been successfully swapped without using a temporary variable.

Core XOR Properties

The XOR swap technique depends on the following important properties:

a ^ a = 0
a ^ 0 = a
a ^ b ^ b = a
a ^ b ^ a = b

Therefore, by performing three XOR operations, we can swap two integer numbers in Java without creating a temporary variable.

The complete swapping logic is:

firstNumber = firstNumber ^ secondNumber;
secondNumber = firstNumber ^ secondNumber;
firstNumber = firstNumber ^ secondNumber;

This technique works because the bitwise XOR operator allows us to recover each original value from the combined XOR result.

Complete Java Program to Swap Two Numbers

Here is the complete Java program to swap values of two integer variables using the bitwise XOR operator (^).

public class SwapUsingXOR {

    public static void main(String[] args) {

        // Initialize two integer variables
        int firstNumber = 10;
        int secondNumber = 20;

        // Display the values before the swap
        System.out.println("Before swapping:");
        System.out.println("First number = " + firstNumber);
        System.out.println("Second number = " + secondNumber);

        // Swap two numbers using XOR
        // --- XOR Swap Logic ---
        firstNumber = firstNumber ^ secondNumber;
        secondNumber = firstNumber ^ secondNumber;
        firstNumber = firstNumber ^ secondNumber;

        // Display the values after the swap
        System.out.println("\nAfter swapping:");
        System.out.println("First number = " + firstNumber);
        System.out.println("Second number = " + secondNumber);
    }
}

Expected Output:

Before swapping:
First number = 10
Second number = 20

After swapping:
First number = 20
Second number = 10

XOR Swap Logic Explained with Binary Representation

The bitwise XOR (^) operator outputs 1 when comparing two different bits and 0 when comparing identical bits.

Initial Binary Values (8-bit format)

  • firstNumber = 10 → 0000 1010
  • secondNumber = 20 → 0001 0100

Step 1: Combine bit patterns

firstNumber = firstNumber ^ secondNumber;

  0000 1010 (firstNumber = 10)
^ 0001 0100 (secondNumber = 20)
---------------------------------
0001 1110 (firstNumber = 30)

Step 2: Extract original firstNumber into secondNumber

secondNumber = firstNumber ^ secondNumber;

  0001 1110 (firstNumber = 30)
^ 0001 0100 (secondNumber = 20)
---------------------------------
0000 1010 (secondNumber = 10)

Step 3: Extract original secondNumber into firstNumber

firstNumber = firstNumber ^ secondNumber;

  0001 1110 (firstNumber = 30)
^ 0000 1010 (secondNumber = 10)
---------------------------------
0001 0100 (firstNumber = 20)

Final Result

firstNumber = 20 (0001 0100)
secondNumber = 10 (0000 1010)
DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.