# Swap two numbers without using third variable using XOR (Bitwise)

## Introduction:

In our previous articles, We have discussed different ways to swap the two numbers. In this article, We are going to discuss the swap two numbers without using third variable here we will use the bitwise XOR Operator to swap numbes.

## Swap two numbers without using third variable ( Swap using Bitwise XOR ):

The Bitwise XOR operator gives us the ability to swap the numbers without using third/extra number.

Before going to discuss the swapping using the bitwise XOR operator. Letâ€™s understand how the bitwise XOR works.

## Bitwise XOR Truth Table:

From the truth table, We can say

• The Bitwise XOR operator returns True only if one of the input is 1 ( Other must be 0)
• XOR Return False, If both the inputs are the same.

## Swap numbers using XOR Operator Algorithm:

Here is the logic to swap number using XOR operator

Above statement is a complex statement, Which is the combination of following statements.

Here is the explanation about above swap logic

1. Step 1, We applyÂ bitwise XORÂ on variable 'a' and variableÂ  'b'.Â And assign the result to variableÂ  'a'.
2. Step 2: We will apply the bitwiseÂ XORÂ on variableÂ  'b' and variableÂ  'a'. And then assign the result to variableÂ  'b'.
3. Step 3: We will perform the bitwiseÂ XORÂ on variableÂ  'a'Â and variableÂ  'b'. And then assign the result to variableÂ  'a'.
4. After performing above three steps, TheÂ values of variable â€˜aâ€™and variable â€˜bâ€™ will be swapped.

## Swap two number using XOR operator Example Walkthrough:

Letâ€™s take two variables 'a'and 'b'.
value of a=11, value of b=22.

At First step, We will perform

a=a^b;

To apply a^b, We need to have variables 'a'and 'b' values in Binary. And then we will apply the bitwise XOR on them.

• Please note, Bitwise operators operates on the bit level, So we need to imagine the numbers in binary format to understand better.

The Binary Value of a(i.e 11) is 00001011

The Binary value of b(i.e 22) is 00010110

Now store above result to variable 'a'. So variable a become 00011101

Now letâ€™s perform the second step b = b^a;

Store the Above (b^a) operation result into the variable 'b'. So Variable 'b' became 00001011

Now perform the third step a = a^b;

Save the result of above a^b operation to variable 'a'.

Now the variable 'a' became 00010110, Which is equal to Decimal value 22.

and the variable 'b' became 00001011, Which is equal to decimal value of 11.

As you can see the values of variable 'a' and variable 'b'are swapped. We are able to swap two numbers without using third number.

## Program : Swap two numbers without using third variable ( Bitwise XOR ):

Letâ€™s convert above XOR swap logic into the code.

## Program Output:

We are using GCC compiler to compile the program on Ubuntu Linux.

ðŸ“¢. Learn More above compiling and running program following article â€“ Hello World Program in C language â€“ SillyCodes

## Conclusion:

In this article, We have learned about the Bitwise XOR operator and how it operates with the input data, and We also learned how to swap two number using the bitwise XOR operator ( without using third/extra variable)