Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Swap two values using XOR operation (http://www.go4expert.com/forums/swap-values-using-xor-operation-t432/)

madhu 30Aug2005 08:26

Swap two values using XOR operation
 
Hi all, How we can Swap the two values using XOR operation but without using third Variable?
Thanks

shabbir 30Aug2005 10:12

Re: Swap two values using XOR operation
 
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

madhu 20Sep2005 19:44

Re: Swap two values using XOR operation
 
Thanks. I was not able to start my PC and so I could not reply.

vignesh1988i 19Sep2009 13:33

Re: Swap two values using XOR operation
 
we can do in a single line using compound assignment tooo....:)..
x^=y^=x^=y;

the above too will work


thank u


All times are GMT +5.5. The time now is 15:39.