DEV Community

Discussion on: How can you swap two variables without using a third?

Collapse
 
kritner profile image
Russ Hammett

No idea if this counts

int x = 4242;
int y = 800000;

x = x ^ y;
y = y ^ x;
x = x ^ y;

I'm sure this can be improved, but trying to get other stuff done ATM :D

working demo: dotnetfiddle.net/hP6T94

Collapse
 
theoutlander profile image
Nick Karnik

Thanks for the working demo! That is the correct solution. :)