~ Office Supplies ~~ Buy Posters ~~ A-Z Products ~~ Website Advertising


Xor swap algorithm - Wikipedia

<<Up     Contents

Xor swap algorithm

Xor swap is a simple algorithm which uses the Exclusive disjunction (XOR) operation to swap the values of two variables without using an extra variable. This algorithm follows (where X and Y are the names of two variables, rather than two values):

  1. XOR the values of X and Y and store the result in X
  2. XOR the values of X and Y and store the result in Y
  3. XOR the values of X and Y and store the result in X

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

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

See: Assembler code, Visual Basic code, C code

wikipedia.org dumped 2003-03-17 with terodump




 
 
7 grams reddish pink maroon UNAKITE cabbing tumbled polished rough jewelry gemstones 38 carat Lovely
 7 grams reddish pink maroon UNAKITE cabbing tumbled polished jewelry 38 carat Lovely 
 
6 carat Golden canary yellow SCAPOLITE gem stone Facet rough gemstone Faceting gems jewel crystal 3
 6 carat Golden canary yellow SCAPOLITE ing jewel crystal 3 
 
Light green butter new jade Serpentine gem stone polished faceted Briolette stringing bead single
 Light green butter new jade Serpentine polished ed Briolette stringing bead single 
 
7 carat RARE yellow orange CLINOHUMITE gem stone crystal cabbing lapidary cabochon rough 1 gram B
 7 carat RARE yellow orange CLINOHUMITE crystal cabbing lapidary cabochon 1 gram B 
 
24 carat blue green Amazonite rough gemstone tumble polished nugget drilled necklace bead Nice 4 gr
 24 carat blue green Amazonite tumble polished nugget drilled necklace bead Nice 4 gr