Teachers Paradise School Supplies Teacher Resources Free Encyclopedia
Teachers Paradise FREE Teaching Resources
Home Arts Crafts Audio Visual Equipment Office Supplies Teacher Resources
Main Page | Edit this page

Xor swap algorithm

In computer programming, the xor swap is an algorithm which uses the exclusive disjunction (XOR) operation to swap the values of two variables without a temporary variable. This algorithm works using the symmetric difference property of XOR, that
A xor A = 0 for every A

Table of contents
1 Explanation
2 Usage in practice

Explanation

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):
  1. XOR X and Y and store in X
  2. XOR X and Y and store in Y
  3. XOR X and Y and store in X

The algorithm looks much simpler when it is written in
pseudocode.
X := X XOR Y
Y := X XOR Y
X := X XOR Y

This typically corresponds to three machine code instructions. For example, in IBM 370 Assembler code:
XOR R1, R2
XOR R2, R1
XOR R1, R2

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.

To see how this works, call the initial value of X = x0 and the initial value of Y = y0. Then:

  1. X = x0 XOR y0, Y = y0
  2. X = x0 XOR y0, Y = x0 XOR y0 XOR y0 = x0 XOR 0 = x0
  3. X = x0 XOR y0 XOR x0 = x0 XOR x0 XOR y0 = 0 XOR y0 = y0, Y = x0

(remember that a XOR a=0, b XOR 0=b).

Code examples

x86 assembly language

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, BX

However, all x86 microprocessors have an XCHG instruction which does the same work on its operands more efficiently than the above sequence of XORs.

Visual Basic

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 Sub

C

C code to implement XOR swap of x and y:

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

The 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;
 }

Usage in practice

Some experienced programmers advise that this technique should not be used in programming, as its effect is not clear unless you already know the trick. However, others remark that its use is acceptable providing that the trick is wrapped in an appropriately named macro such as SWAP or XOR_SWAP, or appropriate commenting of the code is done.

The use of the algorithm is not uncommon in embedded assembly code where often there is 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.

This trick could also be used by someone trying to win an Obfuscated C Code Contest.

See also: xor, symmetric difference, xor linked list




Pay for Educational Supplies & Teaching Supplies with Visa, Master Card, American Express, Discover or Paypal.
TeachersParadise.com HOME | Safe Shopping Guarantee | Help Desk
All trademarks & brands are the property of their respective owners.
Legal Notice 2000-2008 TeachersParadise.com, Inc. All Rights Reserved