Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
XOR swap algorithm
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Variations== The underlying principle of the XOR swap algorithm can be applied to any operation meeting criteria L1 through L4 above. Replacing XOR by addition and subtraction gives various slightly different, but largely equivalent, formulations. For example:<ref>{{cite book |last1=Warren |first1=Henry S. |title=Hacker's delight |date=2003 |publisher=Addison-Wesley |location=Boston |isbn=0201914654 |page=39}}</ref> <syntaxhighlight lang="c"> void AddSwap( unsigned int* x, unsigned int* y ) { *x = *x + *y; *y = *x - *y; *x = *x - *y; } </syntaxhighlight> Unlike the XOR swap, this variation requires that the underlying processor or programming language uses a method such as [[modular arithmetic]] or [[bignum]]s to guarantee that the computation of <code>X + Y</code> cannot cause an error due to [[integer overflow]]. Therefore, it is seen even more rarely in practice than the XOR swap. However, the implementation of <code>AddSwap</code> above in the C programming language always works even in case of integer overflow, since, according to the C standard, addition and subtraction of unsigned integers follow the rules of [[modular arithmetic]], i. e. are done in the [[cyclic group]] <math>\mathbb Z/2^s\mathbb Z</math> where <math>s</math> is the number of bits of <code>unsigned int</code>. Indeed, the correctness of the algorithm follows from the fact that the formulas <math>(x + y) - y = x</math> and <math>(x + y) - ((x + y) - y) = y</math> hold in any [[abelian group]]. This generalizes the proof for the XOR swap algorithm: XOR is both the addition and subtraction in the abelian group <math>(\mathbb Z/2\mathbb Z)^{s}</math> (which is the [[direct sum]] of ''s'' copies of <math>\mathbb Z/2\mathbb Z</math>). This doesn't hold when dealing with the <code>signed int</code> type (the default for <code>int</code>). Signed integer overflow is an undefined behavior in C and thus modular arithmetic is not guaranteed by the standard, which may lead to incorrect results. The sequence of operations in <code>AddSwap</code> can be expressed via matrix multiplication as: :<math> \begin{pmatrix}1 & -1\\0 & 1\end{pmatrix} \begin{pmatrix}1 & 0\\1 & -1\end{pmatrix} \begin{pmatrix}1 & 1\\0 & 1\end{pmatrix} = \begin{pmatrix}0 & 1\\1 & 0\end{pmatrix} </math>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
XOR swap algorithm
(section)
Add topic