Swap using XOR

Swap Two Numbers using XOR in C++

C++Intermediate
C++
#include <iostream>
using namespace std;

void swapXOR(int& a, int& b) {
    a = a ^ b;
    b = a ^ b;  // b = (a^b) ^ b = a
    a = a ^ b;  // a = (a^b) ^ a = b
}

int main() {
    int a = 10, b = 20;
    
    cout << "Before swap: a = " << a << ", b = " << b << endl;
    
    swapXOR(a, b);
    
    cout << "After swap: a = " << a << ", b = " << b << endl;
    
    // Multiple swaps
    cout << "\nMultiple swaps:" << endl;
    int x = 5, y = 15, z = 25;
    
    cout << "Before: x = " << x << ", y = " << y << ", z = " << z << endl;
    
    swapXOR(x, y);
    swapXOR(y, z);
    
    cout << "After: x = " << x << ", y = " << y << ", z = " << z << endl;
    
    // XOR properties demonstration
    cout << "\nXOR properties:" << endl;
    int num1 = 12, num2 = 25;
    cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
    cout << "num1 ^ num2 = " << (num1 ^ num2) << endl;
    cout << "(num1 ^ num2) ^ num2 = " << ((num1 ^ num2) ^ num2) << " (back to num1)" << endl;
    cout << "(num1 ^ num2) ^ num1 = " << ((num1 ^ num2) ^ num1) << " (back to num2)" << endl;
    
    return 0;
}

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)

This program teaches you how to Swap Two Numbers using XOR in C++. The XOR swap algorithm is an elegant bit manipulation technique that swaps two values without using a temporary variable. It demonstrates the unique properties of the XOR operation.


1. What This Program Does

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.


2. Header Files Used

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

3. 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

4. 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

5. 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

6. 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

7. 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)

8. 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

9. 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

10. return 0;

This ends the program successfully.


Summary

  • XOR swap: swaps two numbers without temporary variable using XOR properties.
  • Algorithm: a = a^b, b = a^b (recover a), a = a^b (recover b).
  • Works because: (a^b)^b = a and (a^b)^a = b (XOR reversibility).
  • Important: don't use if variables point to same memory location.
  • Understanding XOR swap demonstrates XOR properties and bit manipulation.
  • Useful in embedded systems, though modern compilers optimize standard swaps.

This program is fundamental for learning XOR properties, understanding bit manipulation techniques, and preparing for advanced bit manipulation algorithms in C++ programs.