Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. /*
  2. Q: Swap two numbers without using a temporary.
  3.  
  4. A: You can use either an XOR approach or a combination of additions and subtractions.
  5. */
  6.  
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. void swap(int &a, int &b) {
  11. a = a ^ b;
  12. b = a ^ b;
  13. a = a ^ b;
  14. }
  15.  
  16. int main() {
  17. int a = 5, b = 10;
  18. cout << "a: " << a << endl;
  19. cout << "b: " << b << endl;
  20. swap(a, b);
  21. cout << "New a: " << a << endl;
  22. cout << "New b: " << b << endl;
  23. return 0;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement