jasonpogi1669

Swap 2 numbers using XOR in C++

Dec 15th, 2021 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     ios::sync_with_stdio(false);
  7.     cin.tie(0);
  8.     int a, b;
  9.     cin >> a >> b;
  10.     cout << "Before:" << '\n';
  11.     cout << a << " " << b << '\n';
  12.     a ^= b;
  13.     b ^= a;
  14.     a ^= b;
  15.     cout << "After:" << '\n';
  16.     cout << a << " " << b << '\n';
  17.     return 0;
  18. }
  19.  
Add Comment
Please, Sign In to add comment