Swap two values using XOR operation

madhu's Avatar
Light Poster
Hi all, How we can Swap the two values using XOR operation but without using third Variable?
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Very simple
int x = 1;
int y = 10;
x = x^y;
y = x^y;
x = x^y;
This is similar to doing with +- or */

Shabbir Bhimani
madhu's Avatar
Light Poster
Thanks. I was not able to start my PC and so I could not reply.
vignesh1988i's Avatar
we can do in a single line using compound assignment tooo......

the above too will work

thank u
SaswatPadhi like this