Suppose you have two variables x and y and you want to swap their values. The usual way to do this is using another temporary variable:
temp = x;
x = y;
y = temp;
However, the interchange of two variables can be done without the temp variable:
x = x + y;
y = x - y;
x = x - y;
What is your favourite algorithm for swapping variables?
x ^= y ^= x ^= y;
though this exact wording is not recommended.
it is do not work in java. this code in C++.
t = x;
x = y;
y = t;
+/- "solution" leads us to integer overflow. Really, best solution is swapping with temporary variable, and variant a ^= b ^= a ^= b (which have undefined behavior following C standard) is useful only when you have been taken interview for your new work, and have got task "swap two variables without using additional memory).
Also, good variant is std::swap. You code will be at least - readable.
Integer overflow in not important in this case.
btw you posted comment as russian
x = y;
y = t;
else
p = x;
x = y;
y = t;
Oh, sorry... my bad English... it`s in past time...
I say, that swap can swapping structs and other variable...