Jump to content

XOR swap algorithm

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Rlee0001 (talk | contribs) at 20:47, 9 November 2002 (Created article. Needs Copy-editing.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Xor swap is a simple algorithm which swaps the values of two variables without using a buffer. This algorithm follows:

  1. XOR the value of variable-one with variable-two and store the value in variable-one
  2. XOR the value of variable-one with variable-two and store the value in variable-two
  3. XOR the value of variable-one with variable-two and store the value in variable-one

This algorithm varies from other swap algorithms only in that it doesn't need to store the original value of variable-one in a temporary buffer prior to overwriting it and doesn't need to restore this value in variable-two.

See: Xor swap algorithm/Assembler Code