Xor swap is a simple algorithm which uses the Exclusive disjunction (XOR) operation to swap the values of two variables without using an extra variable. This algorithm follows (where X and Y are the names of two variables, rather than two values):
To see how this works, call the initial value of X = x0 and the initial value of Y = y0. Then:
This algorithm varies from other swap algorithms only in that it doesn't need to store the original value of X in a temporary buffer prior to overwriting it and doesn't need to restore this value in Y.
See: Assembler code, Visual Basic code, C code