| • Science | • People | • Locations | • Timeline |
| Contents | ||
Standard swapping algorithms require the use of a temporary storage area - standard intuitive algorithms to swap x and y involve:
However, the XOR swap algorithm does not -- this algorithm follows (where X and Y are the names of two variables, rather than two values):
The algorithm looks much simpler when it is written in pseudocode.
This typically corresponds to three machine code instructions. For example, in IBM 370 Assembler code:
where R1, and R2 are registers and the operation XOR leaves the result in the first argument. This algorithm is particularly attractive to assembler programmers due to its performance and efficiency. It eliminates the usage of the intermediate register which is a limited resource in machine language programming. It also eliminates two memory access cycles which would be expensive compared to a register operation.
For example, let's say we have two values X = 12 and Y = 10. In binary, we have
Now, we XOR X and Y to get 0 1 1 0 and store in X. We now have
XOR X and Y again to get 1 1 0 0 - store in Y, and we now have
XOR X and Y again to get 1 0 1 0 - store in X, and we have
The values are swapped, and the algorithm has indeed worked in this instance.
In general, however, if we call the initial value of X = x and the initial value of Y = y, then performing the above steps, using ⊕ for XOR for clarity, and remembering that a ⊕ a = 0 and b ⊕ 0 = b, yields:
The following code in x86 assembly language uses the xor swap algorithm to swap the value in the AX register with the value in the BX register without using a temporary buffer.
XOR AX, BX XOR BX, AX XOR AX, BXHowever, all x86 microprocessors have an XCHG instruction which does the same work on its operands more efficiently than the above sequence of XORs.
The following Visual Basic subroutine swaps the values of its parameters using the xor operator.
Sub Swap (Var1, Var2) Var1 = Var1 Xor Var2 Var2 = Var2 Xor Var1 Var1 = Var1 Xor Var2 End SubThe set-up as a compiler macro is common for extensive use.
#define xorSwap(x,y) {(x)=(x)^(y); (y)=(x)^(y); (x)=(x)^(y);}As a function:
void xorSwap(int *x, int *y) { *x = *x ^ *y; *y = *x ^ *y; *x = *x ^ *y; }It should be noted that this function will not work if you try to swap something with itself (ie here xorSwap(&var, &var) will fail - it will assign the value zero to var).
The use of the algorithm is not uncommon in embedded assembly code where there is often very limited space available for temporary swap space, and this form of swap can also avoid a load/store which can make things much faster. Some optimizing compilers can generate code using this algorithm.
However on modern (desktop) CPUs, the XOR technique is considerably slower than using a temporary variable to do swapping. This is because modern CPUs strive to execute commands in parallel. In the XOR technique, the operands of all commands depend on the results of the previous command so they must be executed in strictly sequential order. If efficiency is of tremendous concern, it is advised to test the speeds of both the XOR technique and temporary variable swapping on the target architecture.
If the language permits, the ugly details of swapping should be hidden inside a macro or an inlined function. Not only will it make the code clearer, but it will also be possible to switch to a different swapping routine if it is faster.
This trick could also be used by someone trying to win an Obfuscated C Code Contest.