Skip to content

Commit a7937e8

Browse files
amuzaldathuva4
authored andcommitted
Added XorSwap implementation in C++ (thuva4#511)
1 parent 1b8c166 commit a7937e8

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

XorSwap/C++/XorSwap.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
void xorSwap (int *x, int *y) {
6+
if (x != y) {
7+
*x ^= *y;
8+
*y ^= *x;
9+
*x ^= *y;
10+
}
11+
}
12+
13+
int main(){
14+
int a,b;
15+
a=10;
16+
b=45;
17+
18+
cout<<"values before swap :\n";
19+
cout<<"a = "<<a<<endl;
20+
cout<<"b = "<<b<<endl;
21+
22+
xorSwap(&a,&b);
23+
24+
cout<<"values after swap :\n";
25+
26+
cout<<"a = "<<a<<endl;
27+
cout<<"b = "<<b;
28+
}

0 commit comments

Comments
 (0)