# Blog Archives

## XOR swap algorithm

http://en.wikipedia.org/wiki/XOR_swap_algorithm

Conventional swapping requires the use of a temporary storage variable. Using the XOR swap algorithm, however, no temporary storage is needed. The algorithm is as follows:

X := X XOR Y

Y := X XOR Y

X := X XOR Y

To understand it, think about the PLUS swap algorithm

a = a + b

b = a – b

a = a -b

interpret XOR: it is a binary PLUS operation without carry.

Advertisements