Given two variables, x and y, swap two variables without using a third variable.

Method 1 (Using Arithmetic Operators)
The idea is to get sum in one of the two given numbers. The numbers can then be swapped using the sum and subtraction from sum.

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0Aint%20main()%0A%7B%0A%20%20int%20x%20%3D%2010%2C%20y%20%3D%205%3B%0A%20%0A%20%20%2F%2F%20Code%20to%20swap%20’x’%20and%20’y’%0A%20%20x%20%3D%20x%20%2B%20y%3B%20%20%2F%2F%20x%20now%20becomes%2015%0A%20%20y%20%3D%20x%20-%20y%3B%20%20%2F%2F%20y%20becomes%2010%0A%20%20x%20%3D%20x%20-%20y%3B%20%20%2F%2F%20x%20becomes%205%0A%20%0A%20%20printf(%22After%20Swapping%3A%20x%20%3D%20%25d%2C%20y%20%3D%20%25d%22%2C%20x%2C%20y)%3B%0A%20%0A%20%20return%200%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

Output:

After Swapping: x = 5, y = 10

Multiplication and division can also be used for swapping.

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0Aint%20main()%0A%7B%0A%20%20int%20x%20%3D%2010%2C%20y%20%3D%205%3B%0A%20%0A%20%20%2F%2F%20Code%20to%20swap%20’x’%20and%20’y’%0A%20%20x%20%3D%20x%20*%20y%3B%20%20%2F%2F%20x%20now%20becomes%2050%0A%20%20y%20%3D%20x%20%2F%20y%3B%20%20%2F%2F%20y%20becomes%2010%0A%20%20x%20%3D%20x%20%2F%20y%3B%20%20%2F%2F%20x%20becomes%205%0A%20%0A%20%20printf(%22After%20Swapping%3A%20x%20%3D%20%25d%2C%20y%20%3D%20%25d%22%2C%20x%2C%20y)%3B%0A%20%0A%20%20return%200%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

Output:

After Swapping: x = 5, y = 10
[ad type=”banner”]

Method 2 (Using Bitwise XOR)
The bitwise XOR operator can be used to swap two variables. The XOR of two numbers x and y returns a number which has all the bits as 1 wherever bits of x and y differ. For example XOR of 10 (In Binary 1010) and 5 (In Binary 0101) is 1111 and XOR of 7 (0111) and 5 (0101) is (0010).

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0Aint%20main()%0A%7B%0A%20%20int%20x%20%3D%2010%2C%20y%20%3D%205%3B%0A%20%0A%20%20%2F%2F%20Code%20to%20swap%20’x’%20(1010)%20and%20’y’%20(0101)%0A%20%20x%20%3D%20x%20%5E%20y%3B%20%20%2F%2F%20x%20now%20becomes%2015%20(1111)%0A%20%20y%20%3D%20x%20%5E%20y%3B%20%20%2F%2F%20y%20becomes%2010%20(1010)%0A%20%20x%20%3D%20x%20%5E%20y%3B%20%20%2F%2F%20x%20becomes%205%20(0101)%0A%20%0A%20%20printf(%22After%20Swapping%3A%20x%20%3D%20%25d%2C%20y%20%3D%20%25d%22%2C%20x%2C%20y)%3B%0A%20%0A%20%20return%200%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

Output:

After Swapping: x = 5, y = 10

Problems with above methods
1) The multiplication and division based approach doesn’ work if one of the numbers is 0 as the product becomes 0 irrespective of the other number.

2) Both Arithmetic solutions may cause arithmetic overflow. If x and y are too large, addition and multiplication may go out of integer range.

3) When we use pointers to variable and make a function swap, all of the above methods fail when both pointers point to the same variable. Let’s take a look what will happen in this case if both are pointing to the same variable.

// Bitwise XOR based method
x = x ^ x; // x becomes 0
x = x ^ x; // x remains 0
x = x ^ x; // x remains 0

// Arithmetic based method
x = x + x; // x becomes 2x
x = x – x; // x becomes 0
x = x – x; // x remains 0

[ad type=”banner”]

Let us see the following program.

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0Avoid%20swap(int%20*xp%2C%20int%20*yp)%0A%7B%0A%20%20%20%20*xp%20%3D%20*xp%20%5E%20*yp%3B%0A%20%20%20%20*yp%20%3D%20*xp%20%5E%20*yp%3B%0A%20%20%20%20*xp%20%3D%20*xp%20%5E%20*yp%3B%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20int%20x%20%3D%2010%3B%0A%20%20swap(%26x%2C%20%26x)%3B%0A%20%20printf(%22After%20swap(%26x%2C%20%26x)%3A%20x%20%3D%20%25d%22%2C%20x)%3B%0A%20%20return%200%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

Output:

After swap(&x, &x): x = 0

Swapping a variable with itself may needed in many standard algorithms. For example see this implementation of QuickSort where we may swap a variable with itself. The above problem can be avoided by putting a condition before the swapping.

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0Avoid%20swap(int%20*xp%2C%20int%20*yp)%0A%7B%0A%20%20%20%20if%20(xp%20%3D%3D%20yp)%20%2F%2F%20Check%20if%20the%20two%20addresses%20are%20same%0A%20%20%20%20%20%20return%3B%0A%20%20%20%20*xp%20%3D%20*xp%20%2B%20*yp%3B%0A%20%20%20%20*yp%20%3D%20*xp%20-%20*yp%3B%0A%20%20%20%20*xp%20%3D%20*xp%20-%20*yp%3B%0A%7D%0Aint%20main()%0A%7B%0A%20%20int%20x%20%3D%2010%3B%0A%20%20swap(%26x%2C%20%26x)%3B%0A%20%20printf(%22After%20swap(%26x%2C%20%26x)%3A%20x%20%3D%20%25d%22%2C%20x)%3B%0A%20%20return%200%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

Output:

After swap(&x, &x): x = 10
[ad type=”banner”]

Categorized in: