Swap two values using XOR operation

Light Poster
30Aug2005,08:26   #1
madhu's Avatar
Hi all, How we can Swap the two values using XOR operation but without using third Variable?
Thanks
Go4Expert Founder
30Aug2005,10:12   #2
shabbir's Avatar
Very simple
Code:
int x = 1;
int y = 10;
x = x^y;
y = x^y;
x = x^y;
This is similar to doing with +- or */

Thanks
Shabbir Bhimani
Light Poster
20Sep2005,19:44   #3
madhu's Avatar
Thanks. I was not able to start my PC and so I could not reply.
Banned
19Sep2009,13:33   #4
vignesh1988i's Avatar
we can do in a single line using compound assignment tooo......
x^=y^=x^=y;

the above too will work


thank u
SaswatPadhi likes this