Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Q: Swap two numbers without using a temporary.
- A: You can use either an XOR approach or a combination of additions and subtractions.
- */
- #include <iostream>
- using namespace std;
- void swap(int &a, int &b) {
- a = a ^ b;
- b = a ^ b;
- a = a ^ b;
- }
- int main() {
- int a = 5, b = 10;
- cout << "a: " << a << endl;
- cout << "b: " << b << endl;
- swap(a, b);
- cout << "New a: " << a << endl;
- cout << "New b: " << b << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement