How To Swap Two Variables Without Using Third Temporary Variable ?

Link Copied To Clipboard !

how-to-swap-variables-without-temp Programming

You can use either addition/subtraction or multiplication/division or bitwise XOR or mixture of above mentioned methods to swap two variables without using the third temporary variable. Swapping is the process of exchanging values of two variables. For example, if variable a = 5 and variable b = 10, after swapping, the variable values will be a = 10 and b = 5.

Before going to the solution, let’s first discuss how we can swap two variables using the most general and easiest method, that is, by using temporary variable.

How to swap variables ?

Two variables can be swapped by using a third temporary variable, storing the value of one of the variable temporarily and setting it to one later.

int a = 5;
int b = 10;
//let's swap by using temp variable
int temp = a; //store value of a to temp
//now swap
a = b;
b = temp;//temp is actually equal to the value of a

//at this point, variables have been swapped

But, this is not what we want. We want to swap two variables without using temp variable right. Let’s see how we can do it.

How to swap variables without temp variable ?

We can use either addition/subtraction or multiplication/division or bitwise XOR or mixture of above mentioned methods to swap two variables without using the third temporary variable.

Swapping variables using addition-subtraction method

In this method, first two variables are added and assigned to one already defined variable. The later variable is the assigned to difference between added sum and variable itself. At this point, one variable has already been assigned with swapped value. Now, same difference technique is used to set the value of first variable. For example, let’s say x = 5 and y = 10. Now, x and y are added and assigned to x itself. This means x is now, x = 15. Variable y is then set to a value resulted from subtraction of x and y. Meaning, y = x – y = 15 – 10 = 5. Again, same process is applied to x, x = x – y = 15 – 5 = 10. Hence, the variables have been swapped.

int a = 5;
int b = 10;

a = a + b; //a is now 15
b = a - b; //b is now 5
a = a - b; //a is now 10

//at this point, variables have been swapped

Swapping variables using multiplication-division method

In this method, first two variables are multiplied and assigned to one already defined variable. The later variable is the assigned to division between multiplied result and variable itself. At this point, one variable has already been assigned with swapped value. Now, same division technique is used to set the value of first variable. For example, let’s say x = 5 and y = 10. Now, x and y are multiplied and assigned to x itself. This means x is now, x = 50. Variable y is then set to a value resulted from division of x and y. Meaning, y = x / y = 50 / 10 = 5. Again, same process is applied to x, x = x / y = 50 / 5 = 10. Hence, the variables have been swapped.

int a = 5;
int b = 10;

a = a * b; //a is now 50
b = a / b; //b is now 5
a = a / b; //a is now 10

//at this point, variables have been swapped

Note: This is method does not work when one of the variable value is zero. Because, it will cause the multiplication to be zero no matter the value of other. Also, the division might result an infinite value.

Swapping variables using bitwise XOR method

In this method, bitwise XOR is used to swap two variables. The result of XOR of two bits is 1 whenever those two bits are different. Meaning that, XOR of 1 and 0 or 0 and 1 is 1 but XOR of 1 and 1 or 0 and 0 is always 0. Variables are converted into their respective bits and XOR is performed on them. XOR is again performed between two to get the value for second variable. Again, XOR between first and second and performed to get the value for first variable. It can be better explained by following code.

int a = 5; //0101
int b = 10; //1010

a = a ^ b; //a is now 1111
b = a ^ b; //b is now 0101
a = a / b; //a is now 1010

//at this point, variables have been swapped

Additionally, you may utilize the above mentioned methods to create another hybrid method which used arithmetic and bitwise operation to swap variables.


You May Also Like