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

swap-numbers-without-third-number-using-bitwise-xor-operator

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:

Bit A ValueBit B ValueA ^ B ( A XOR B )
000
101
011
110

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 aand 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)

Related Articles:

Related Programs:

Learn More about Binary Numbers:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] we are using a temp variable to do the swapping, But we can also use Swapping without using the temporary variable method. for simplicity, we are using the temporary variable to swap the […]

Leave a Reply