C++ Program to Swap Two Numbers Using XOR Operator
You can swap two numbers in C++ without a temporary variable using XOR with
a = a ^ b;, b = a ^ b;, and a = a ^ b;.Examples
Inputa = 5, b = 10
OutputAfter swap: a = 10, b = 5
Inputa = 0, b = 0
OutputAfter swap: a = 0, b = 0
Inputa = -3, b = 7
OutputAfter swap: a = 7, b = -3
How to Think About It
To swap two numbers using XOR, think of XOR as a way to combine bits so you can store both values in the same variables without extra space. By applying XOR three times in the right order, you can exchange the values without a temporary variable.
Algorithm
1
Take two numbers a and b.2
Apply XOR between a and b and store the result in a.3
Apply XOR between new a and b and store the result in b.4
Apply XOR between new a and new b and store the result in a.5
Now a and b are swapped.Code
cpp
#include <iostream> using namespace std; int main() { int a = 5, b = 10; cout << "Before swap: a = " << a << ", b = " << b << endl; a = a ^ b; b = a ^ b; a = a ^ b; cout << "After swap: a = " << a << ", b = " << b << endl; return 0; }
Output
Before swap: a = 5, b = 10
After swap: a = 10, b = 5
Dry Run
Let's trace swapping a=5 and b=10 using XOR.
1
Initial values
a = 5 (0101), b = 10 (1010)
2
a = a ^ b
a = 0101 ^ 1010 = 1111 (15), b = 10
3
b = a ^ b
b = 1111 ^ 1010 = 0101 (5), a = 15
4
a = a ^ b
a = 1111 ^ 0101 = 1010 (10), b = 5
| Step | a (decimal) | b (decimal) |
|---|---|---|
| Initial | 5 | 10 |
| After a = a ^ b | 15 | 10 |
| After b = a ^ b | 15 | 5 |
| After a = a ^ b | 10 | 5 |
Why This Works
Step 1: First XOR stores combined info
The first a = a ^ b stores the combined bits of both numbers in a.
Step 2: Second XOR extracts original a
The second b = a ^ b uses the combined bits to get the original a value into b.
Step 3: Third XOR extracts original b
The last a = a ^ b extracts the original b value into a, completing the swap.
Alternative Approaches
Using a temporary variable
cpp
#include <iostream> using namespace std; int main() { int a = 5, b = 10, temp; temp = a; a = b; b = temp; cout << "After swap: a = " << a << ", b = " << b << endl; return 0; }
This method is simple and clear but uses extra memory for a temporary variable.
Using addition and subtraction
cpp
#include <iostream> using namespace std; int main() { int a = 5, b = 10; a = a + b; b = a - b; a = a - b; cout << "After swap: a = " << a << ", b = " << b << endl; return 0; }
This method avoids extra memory but can cause overflow with large numbers.
Complexity: O(1) time, O(1) space
Time Complexity
The XOR swap uses a fixed number of operations (three XORs), so it runs in constant time.
Space Complexity
It uses no extra memory besides the input variables, so space complexity is constant.
Which Approach is Fastest?
All methods run in constant time, but XOR swap avoids extra memory. However, using a temporary variable is clearer and safer.
| Approach | Time | Space | Best For |
|---|---|---|---|
| XOR Swap | O(1) | O(1) | Memory-constrained environments |
| Temporary Variable | O(1) | O(1) | Readability and safety |
| Addition/Subtraction | O(1) | O(1) | Avoiding extra variables but careful with overflow |
Use XOR swap only when you want to avoid extra memory and know the variables are not the same memory location.
Trying XOR swap on the same variable (e.g., swapping a variable with itself) will zero it out.