# 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

Posted on September 30, 2013, in Algorithms and tagged XOR swap algorithm. Bookmark the permalink. 1 Comment.

Pingback: XOR swap algorithm | Ragnarok Connection