Swap using XOR
Swap Two Numbers using XOR in C++
C++ Swap using XOR Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}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)
Understanding Swap using XOR
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:
XOR swap is a clever bit manipulation technique.
---
2. Header Files Used
---
3. Understanding XOR Swap
XOR Properties
:
Swap Algorithm
:
---
4. XOR Swap Algorithm
Three Steps
:
How it works
:
---
5. Why It Works
Mathematical Proof
:
XOR Reversibility
:
---
6. Important Limitation
Same Memory Location
:
Example Problem
:
---
7. When to Use XOR Swap
Best For
:
Not Recommended For
:
---
8. Modern Compilers
Compiler Optimization
:
Recommendation
:
---
9. XOR Properties Demonstration
Reversibility
:
Understanding
:
---
10. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning XOR properties, understanding bit manipulation techniques, and preparing for advanced bit manipulation algorithms in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Swap using XOR
This C++ program is part of the "Bitwise Operations Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.