Rosetta Code talk:Village Pump/CS Pages Wanted

From Rosetta Code
Revision as of 09:14, 17 March 2009 by rosettacode>Dmitry-kazakov (XOR is not cool)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Swapping in a ring

XOR swap is limited to the modulus of 2n. A more general swap is based on addition and subtraction, or equivalently on addition and negative inverse in the ring. This one works for any modulus. Here is an example in Ada. Given <lang ada> type N is mod 5; -- Ring of 0,1,2,3,4 X, Y : N; </lang> Swap X and Y: <lang ada> X := X + Y; Y := X - Y; X := X - Y; </lang> 5 is not a power of two, yet it still works. --Dmitry-kazakov 09:14, 17 March 2009 (UTC)