0% found this document useful (0 votes)
76 views

XOR Swap Algorithm: 2 Proof of Correctness

The XOR swap algorithm allows two variables to swap values without using a temporary variable by using XOR bitwise operations. It works by setting x to x XOR y, then y to y XOR x, and finally x to x XOR y. This corresponds to elementary matrix operations that swap rows or columns. While compact, the algorithm fails if the variables use the same storage location, overwriting the value with zero. Modern compilers can optimize a standard swap to have similar performance, making the XOR swap rarely needed in practice due to issues with readability, parallelization, and aliasing.

Uploaded by

mps1255
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

XOR Swap Algorithm: 2 Proof of Correctness

The XOR swap algorithm allows two variables to swap values without using a temporary variable by using XOR bitwise operations. It works by setting x to x XOR y, then y to y XOR x, and finally x to x XOR y. This corresponds to elementary matrix operations that swap rows or columns. While compact, the algorithm fails if the variables use the same storage location, overwriting the value with zero. Modern compilers can optimize a standard swap to have similar performance, making the XOR swap rarely needed in practice due to issues with readability, parallelization, and aliasing.

Uploaded by

mps1255
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

XOR swap algorithm

x y That is, if x and y use the same storage location, then the
line:
1010 0011 = 1001 → x
X := X XOR Y
1001 0011 = 1010 → y
sets x to zero (because x = y so X XOR Y is zero) and sets
1001 1010 = 0011 → x y to zero (since it uses the same storage location), causing
x and y to lose their original values.
0011 1010
Using the XOR swap algorithm to exchange nibbles between vari-
ables without the use of temporary storage
2 Proof of correctness
The binary operation XOR over bit strings of length
In computer programming, the XOR swap is an N exhibits the following properties (where ⊕ denotes
algorithm that uses the XOR bitwise operation to swap XOR):[lower-alpha 1]
values of distinct variables having the same data type
without using a temporary variable. “Distinct” means that • L1. Commutativity: A ⊕ B = B ⊕ A
the variables are stored at different memory addresses;
the actual values of the variables do not have to be differ- • L2. Associativity: (A ⊕ B) ⊕ C = A ⊕ (B ⊕ C)
ent.
• L3. Identity exists: there is a bit string, 0, (of length
N) such that A ⊕ 0 = A for any A
• L4. Each element is its own inverse: for each A ,
1 The algorithm A⊕A=0.

Conventional swapping requires the use of a temporary Suppose that we have two distinct registers R1 and R2 as
storage variable. Using the XOR swap algorithm, how- in the table below, with initial values A and B respectively.
ever, no temporary storage is needed. The algorithm is We perform the operations below in sequence, and reduce
as follows:[1][2] our results using the properties listed above.
X := X XOR Y Y := Y XOR X X := X XOR Y
2.1 Linear algebra interpretation
The algorithm typically corresponds to three machine
code instructions. Since XOR is a commutative opera- As XOR can be interpreted as binary addition and a pair
tion, X XOR Y can be replaced with Y XOR X in any of of values can be interpreted as a point in two-dimensional
the lines. When coded in assembly language, this com- space, the steps in the algorithm can be interpreted as 2×2
mutativity is often exercised in the second line: matrices with binary values. For simplicity, assume ini-
In the above System/370 assembly code sample, R1 and tially that x and y are each single bits, not bit vectors.
R2 are distinct registers, and each XR operation leaves its For example, the step:
result in the register named in the first argument. Using
X := X XOR Y
x86 assembly, values X and Y are in registers eax and ebx
(respectively), and xor places the result of the operation
in the first register. which also has the implicit:
However, the algorithm fails if x and y use the same stor- Y := Y
age location, since the value stored in that location will be
zeroed out by the first XOR instruction, and then remain corresponds to the matrix ( 1 1 ) as
01
zero; it will not be “swapped with itself”. Note that this
is not the same as if x and y have the same values. The
( )( ) ( )
trouble only comes when x and y use the same storage lo- 1 1 x x+y
cation, in which case their values must already be equal. = .
0 1 y y

1
2 5 REASONS FOR AVOIDANCE IN PRACTICE

The sequence of operations is then expressed as: 4 Reasons for use in practice
In most practical scenarios, the trivial swap algorithm us-
( )( )( ) ( ) ing a temporary register is more efficient. Limited situa-
1 1 1 0 1 1 0 1
= tions in which XOR swapping may be practical include:
0 1 1 1 0 1 1 0
• on a processor where the instruction set encoding
(working with binary values, so 1 + 1 = 0 ), which ex- permits the XOR swap to be encoded in a smaller
presses the elementary matrix of switching two rows (or number of bytes
columns) in terms of the transvections (shears) of adding
one element to the other. • in a region with high register pressure, it may allow
the register allocator to avoid spilling a register
To generalize to where X and Y are not single bits, but
instead bit vectors of length n, these 2×2 (matrices
) are re- • in microcontrollers where available RAM is very
placed by 2n×2n block matrices such as I0n IInn . limited.
Note that these matrices are operating on values, not on
variables (with storage locations), hence this interpreta- Because these situations are rare, most optimizing com-
tion abstracts away from issues of storage location and pilers do not generate XOR swap code.
the problem of both variables sharing the same storage
location.
5 Reasons for avoidance in practice
Most modern compilers can optimize away the temporary
variable in the native swap, in which case the native swap
3 Code example uses the same amount of memory and the same number
of registers as the XOR swap and is at least as fast, and of-
ten faster. The XOR swap is also much less readable and
A C function that implements the XOR swap algorithm:
completely opaque to anyone unfamiliar with the tech-
void xorSwap (int *x, int *y) { if (x != y) { *x ^= *y; *y nique.
^= *x; *x ^= *y; } }
On modern CPU architectures, the XOR technique can
be slower than using a temporary variable to do swap-
Note that the code does not swap the integers passed im- ping. One reason is that modern CPUs strive to execute
mediately, but first checks if their addresses are distinct. instructions in parallel via instruction pipelines. In the
This is because, if the addresses are equal, the algorithm XOR technique, the inputs to each operation depend on
will fold to a triple *x ^= *x resulting in zero. the results of the previous operation, so they must be ex-
It can be done more easily, without checking if the ad- ecuted in strictly sequential order, [3]
negating any benefits
dresses are equal, using this method: of instruction-level parallelism.

/* kcc: “Undefined behavior - Error: EIO8 De- A historical reason was that it used to be patented
scription: unsequenced side effect on scalar ob- (US4197590). Even then, this was only for computer
ject with value computation of same object.” graphics.
See: https://github.com/kframework/c-semantics
and https://runtimeverification.com/match/1.
5.1 Aliasing
0-SNAPSHOT/docs/runningexamples/ There are
no sequence points to force the execution of (*y=*x) last
The XOR swap is also complicated in practice by aliasing.
- see https://en.wikipedia.org/wiki/Sequence_point */
As noted above, if an attempt is made to XOR-swap the
void xorSwap (int *x, int *y) { *x^=*y^(*y=*x); }contents of some location with itself, the result is that the
location is zeroed out and its value lost. Therefore, XOR
The XOR swap algorithm can also be defined with a swapping must not be used blindly in a high-level lan-
macro: guage if aliasing is possible.
#define XORSWAP_UNSAFE(a, b) Similar problems occur with call by name, as in Jensen’s
((a)^=(b),(b)^=(a),(a)^=(b)) /* Doesn't work when Device, where swapping i and A[i] via a temporary vari-
a and b are the same object - assigns zero (0) to the able yields incorrect results due to the arguments being
object in that case */ #define XORSWAP(a, b) ((&(a) == related: swapping via temp = i; i = A[i]; A[i] = temp
&(b)) ? (a) : ((a)^=(b),(b)^=(a),(a)^=(b))) /* checks that changes the value for i in the second statement, which
the addresses of a and b are different before XOR-ing */ then results in the incorrect i value for A[i] in the third
statement.
3

6 Variations 02.

[2] “Swapping Values with XOR”. graphics.stanford.edu.


The underlying principle of the XOR swap algorithm can Retrieved 2014-05-02.
be applied to any operation meeting criteria L1 through
L4 above. Replacing XOR by addition and subtraction [3] Amarasinghe, Saman; Leiserson, Charles (2010). “6.172
gives a slightly different, but largely equivalent, formula- Performance Engineering of Software Systems, Lecture
tion: 2”. MIT OpenCourseWare. Massachusetts Institute of
Technology. Retrieved 27 January 2015.
void addSwap (unsigned int *x, unsigned int *y) { if (x
!= y) { *x = *x + *y; *y = *x - *y; *x = *x - *y; } }

Unlike the XOR swap, this variation requires that the


underlying processor or programming language uses a
method such as modular arithmetic or bignums to guar-
antee that the computation of X + Y cannot cause an error
due to integer overflow. Therefore, it is seen even more
rarely in practice than the XOR swap.
Note, however, that the implementation of addSwap
above in the C programming language always works even
in case of integer overflow, since, according to the C stan-
dard, addition and subtraction of unsigned integers fol-
low the rules of modular arithmetic, i. e. are done in
the cyclic group Z/2s Z where s is the number of bits
of unsigned int. Indeed, the correctness of the algorithm
follows from the fact that the formulas (x + y) − y = x
and (x + y) − ((x + y) − y) = y hold in any abelian
group. This is actually a generalization of the proof for
the XOR swap algorithm: XOR is both the addition and
subtraction in the abelian group (Z/2Z)s .
Please note that the above doesn't hold when dealing
with the signed int type (the default for int). Signed in-
teger overflow is an undefined behavior in C and thus
modular arithmetic is not guaranteed by the standard (a
standard-conforming compiler might optimize out such
code, which leads to incorrect results).

7 See also
• Symmetric difference
• XOR linked list
• Feistel cipher (the XOR swap algorithm is a degen-
erate form of a Feistel cypher)

8 Notes
[1] The first three properties, along with the existence of an
inverse for each element, are the definition of an Abelian
group. The last property is a structural feature of XOR
not necessarily shared by other Abelian groups.

9 References
[1] “The Magic of XOR”. Cs.umd.edu. Retrieved 2014-04-
4 10 TEXT AND IMAGE SOURCES, CONTRIBUTORS, AND LICENSES

10 Text and image sources, contributors, and licenses


10.1 Text
• XOR swap algorithm Source: https://en.wikipedia.org/wiki/XOR_swap_algorithm?oldid=758031061 Contributors: Zundark, The
Anome, Kowloonese, Boleslav Bobcik, Rlee0001, Frecklefoot, Cprompt, Nixdorf, Graue, TakuyaMurata, Delirium, Cyan, Evercat, GRA-
HAMUK, Nikola Smolenski, Timwi, Dcoetzee, Dysprosia, Furrykef, Morwen, Wernher, McKay, Dpbsmith, Ortonmc, Mrjeff, Phil
Boswell, Robbot, Fredrik, Decrypt3, Smjg, Mshonle~enwiki, Mellum, Jason Quinn, Eequor, Neilc, Vadmium, Nova77, Kusunose, An-
dreas Kaufmann, Kcr~enwiki, ZeroOne, Plugwash, Dataphile, CanisRufus, Dgpop, Cmdrjameson, Yonkie, Simetrical, Madmardigan53,
Prophile, Uncle G, Barrylb, GregorB, Gerbrant, Qwertyus, FlaBot, VKokielov, Quuxplusone, Intgr, Chobot, YurikBot, Simoncpu, Zeus-
chu, Misza13, TomJF, LeonardoRob0t, Snaxe920, Erik Sandberg, SmackBot, Meatmanek, Andy M. Wang, Oli Filth, Nbarth, Hooded-
Man, Cybercobra, Jon Awbrey, Jafet, CmdrObot, Christian75, CZeke, Alphachimpbot, Olsonist, Gwern, MartinBot, Themania, Pcordes,
Gall0ws it, Thegargantua, SieBot, Robin479, Mild Bill Hiccup, Alexbot, DumZiBoT, Jaan Vajakas, Dsimic, Addbot, AgadaUrbanit,
AnomieBOT, Piano non troppo, Mklee, PleaseStand, Jesse V., MidgleyC, K6ka, TheGeomaster, Zanyguy, Ptimlick, ClueBot NG, MerlI-
wBot, Nidarshan, BattyBot, Fmadd and Anonymous: 132

10.2 Images
• File:Question_book-new.svg Source: https://upload.wikimedia.org/wikipedia/en/9/99/Question_book-new.svg License: Cc-by-sa-3.0
Contributors:
Created from scratch in Adobe Illustrator. Based on Image:Question book.png created by User:Equazcion Original artist:
Tkgd2007
• File:XOR_Swap.svg Source: https://upload.wikimedia.org/wikipedia/commons/8/8f/XOR_Swap.svg License: Public domain Contribu-
tors: Own work Original artist: PleaseStand

10.3 Content license


• Creative Commons Attribution-Share Alike 3.0

You might also like