Swap using XOR in C++

Swap Two Numbers using XOR in C++.

IntermediateBitwise Operations ProgramsExample 4 of 5
swap-using-xor.cpp
1#include <iostream>
2using namespace std;
3
4void swapXOR(int& a, int& b) {
5 a = a ^ b;
6 b = a ^ b; // b = (a^b) ^ b = a
7 a = a ^ b; // a = (a^b) ^ a = b
8}
9
10int main() {
11 int a = 10, b = 20;
12
13 cout << "Before swap: a = " << a << ", b = " << b << endl;
14
15 swapXOR(a, b);
16
17 cout << "After swap: a = " << a << ", b = " << b << endl;
18
19 // Multiple swaps
20 cout << "\nMultiple swaps:" << endl;
21 int x = 5, y = 15, z = 25;
22
23 cout << "Before: x = " << x << ", y = " << y << ", z = " << z << endl;
24
25 swapXOR(x, y);
26 swapXOR(y, z);
27
28 cout << "After: x = " << x << ", y = " << y << ", z = " << z << endl;
29
30 // XOR properties demonstration
31 cout << "\nXOR properties:" << endl;
32 int num1 = 12, num2 = 25;
33 cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
34 cout << "num1 ^ num2 = " << (num1 ^ num2) << endl;
35 cout << "(num1 ^ num2) ^ num2 = " << ((num1 ^ num2) ^ num2) << " (back to num1)" << endl;
36 cout << "(num1 ^ num2) ^ num1 = " << ((num1 ^ num2) ^ num1) << " (back to num2)" << endl;
37
38 return 0;
39}

Output

Before swap: a = 10, b = 20
After swap: a = 20, b = 10

Multiple swaps:
Before: x = 5, y = 15, z = 25
After: x = 15, y = 25, z = 5

XOR properties:
num1 = 12, num2 = 25
num1 ^ num2 = 21
(num1 ^ num2) ^ num2 = 12 (back to num1)
(num1 ^ num2) ^ num1 = 25 (back to num2)

What's going on

The program demonstrates XOR swap algorithm:

Swapping two numbers without temporary variable
Using XOR properties
Multiple swaps demonstration
XOR properties explanation

XOR swap is a clever bit manipulation technique.

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.

Understanding XOR Swap

XOR Properties

:

a ^ a = 0 (XOR with itself is zero)
a ^ 0 = a (XOR with zero is identity)
(a ^ b) ^ b = a (XOR is reversible)
Commutative: a ^ b = b ^ a

Swap Algorithm

:

Uses XOR properties
No temporary variable needed
Three XOR operations
Elegant solution

XOR Swap Algorithm

Three Steps

:

1.a = a ^ b
2.b = a ^ b // b = (a^b) ^ b = a
3.a = a ^ b // a = (a^b) ^ a = b

How it works

:

Step 1: Store XOR in a
Step 2: Recover original a in b
Step 3: Recover original b in a
Swaps values using XOR properties

Why It Works

Mathematical Proof

:

After step 1: a = a^b, b = b
After step 2: a = a^b, b = (a^b)^b = a
After step 3: a = (a^b)^a = b, b = a
Result: a and b are swapped

XOR Reversibility

:

(a ^ b) ^ b = a (recover a)
(a ^ b) ^ a = b (recover b)
XOR is its own inverse

Important Limitation

Same Memory Location

:

Don't use if variables are same
a ^ a = 0 (would zero the value)
Always check if different
Use temporary for safety

Example Problem

:

swapXOR(x, x) would set x to 0
Must ensure different variables
Check before swapping

When to Use XOR Swap

Best For

:

Embedded systems (limited memory)
Educational purposes
Demonstrating XOR properties
Memory-constrained environments

Not Recommended For

:

Modern systems (compiler optimizes)
Readability concerns
Same variable risk
Production code (usually)

Modern Compilers

Compiler Optimization

:

Modern compilers optimize swaps
Temporary variable optimized away
XOR swap may be slower
Use standard swap for clarity

Recommendation

:

Use std::swap() in production
XOR swap for learning
Understand the concept
Prefer readability

XOR Properties Demonstration

Reversibility

:

(a ^ b) ^ b = a
(a ^ b) ^ a = b
XOR can recover original values
Foundation of XOR swap

Understanding

:

Helps understand algorithm
Demonstrates XOR power
Educational value
Foundation for other algorithms

return 0;

This ends the program successfully.