# Swapping four Variables without a Temporary Variable

By the title "Swapping four variables without a temporary variable," What do you understand?

Let's decode. Here the question is asking us to swap the values of four variables without creating any additional temporary variables. Using a temporary variable to hold one of the values temporarily in various programming languages makes shifting the values of two variables simple. The use of temporary variables, however, becomes ineffective and time−consuming when swapping the values of more than two variables.

## Explanation

Suppose we have four variables a, b, c, and d with the following values:

a = 5 (101)

b = 9 (1001)

c = 12 (1100)

d = 3 (0011)

We want to swap these variables without using a temporary variable.

Here's how we can use the XOR operation to achieve this:

• Compute a = a ^ b ^ c ^ d.

In binary, this is equivalent to:

a = 101 ^ 1001 ^ 1100 ^ 0011

= 0010 (the result of XORing all the bits)

Now a contains the XOR of all four variables.

• Compute d = a ^ b ^ c ^ d.

In binary, this is equivalent to:

d = 0010 ^ 1001 ^ 1100 ^ 0011

= 0100

Now d contains the original value of a.

• Compute c = a ^ b ^ c ^ d.

In binary, this is equivalent to:

c = 0010 ^ 1001 ^ 1100 ^ 0100

= 1111

Now c contains the original value of b.

• Compute b = a ^ b ^ c ^ d.

In binary, this is equivalent to:

b = 0010 ^ 1001 ^ 1111 ^ 0100

= 1010

Now b contains the original value of c.

• Compute a = a ^ b ^ c ^ d.

In binary, this is equivalent to:

a = 0010 ^ 1010 ^ 1111 ^ 0100

= 1101

Now a contains the original value of d.

After these steps, the variables have been swapped without using a temporary variable:

a = 3

b = 12

c = 9

d = 5

## Why use XOR?

The reason for utilizing the XOR operation to swap variables is that it enables us to keep the non−common bits while canceling out the common bits between two variables. This is how it goes:

Let's say we want to swap the values of two variables A and B without using a temporary variable. Using the XOR operator, we can achieve it as follows:

Calculate A = A ^ B.

The bits in A that differ from those in B will be set to 1 in this phase, while the bits that are identical to B will be set to 0.

Calculate B = A ^ B.

We can use A to flip the bits in B to recover A's initial value because it now contains the bits that distinguish it from B.

Calculate A= A^ B

The bits in B that differ from the initial value of A can be flipped using A to switch the values of A and B.

By performing an XOR operation on all the variables, the same reasoning can be expanded to switching more than two variables. The XOR of all the values is obtained by XORing all the variables. With the XOR value and the procedures described above, we can then swap the variables.

In order to swap variables without requiring a temporary variable, the XOR operation allows us to alter the individual bits of a number and cancel out the common bits while keeping the non−common bits.

### Approach

• Take the four variables as user input.

• Use the XOR operation to swap the values of the four variables without using a temporary variable

• Print the swapped values of a, b, c, and d.

## Code Implemetation

### Example

#include <iostream>
using namespace std;

int main() {
int a, b, c, d;

a=5;
b=9;
c=12;
d=3;

// print original values
cout << "Original values: " << a << " " << b << " " << c << " " << d << endl;

// swap values
a = a ^ b ^ c ^ d;
d = a ^ b ^ c ^ d;
c = a ^ b ^ c ^ d;
b = a ^ b ^ c ^ d;
a = a ^ b ^ c ^ d;

// print swapped values
cout << "Swapped values: " << a << " " << b << " " << c << " " << d << endl;

return 0;
}


### Output

Original values: 5 9 12 3
Swapped values: 9 12 3 5


### Complexity

Time Complexity: O(1)

Space Complexty: O(1)

## Conclusion

In summary, swapping the values of four variables without the need of a temporary variable is simple, effective, and elegant when done using the XOR method. This method is well−liked by programmers who place a high priority on efficiency because it enables us to exchange values in situations of constant time and space complexity. By comprehending this idea, you can use it to optimise your code and raise its performance in a variety of situations.

Updated on: 23-Aug-2023

187 Views 