Use the XOR exchange algorithm :
x^=y^=x^=y;
This is equivalent (usually see below):
This works because XORing to the same number twice gives you the original number.
In C ++ 03, the single-expression version is undefined behavior, so it may not work correctly on all compilers / platforms. This is because there is no sequence point between the modification and use of the variables.
In C ++ 11, it is defined correctly. The standard says (5.17.1):
In all cases, the assignment is performed after calculating the value of the right and left operands and before calculating the value of the assignment expression.
source share