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!
==The algorithm== Conventional swapping requires the use of a temporary storage variable. Using the XOR swap algorithm, however, no temporary storage is needed. The algorithm is as follows:<ref>{{cite web|url=http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/xor.html|archive-url=https://web.archive.org/web/20140401081922/http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/xor.html|url-status=dead|archive-date=2014-04-01 |title=The Magic of XOR |publisher=Cs.umd.edu |access-date=2014-04-02}}</ref><ref>{{cite web|url=http://graphics.stanford.edu/~seander/bithacks.html#SwappingValuesXOR|title=Swapping Values with XOR|publisher=graphics.stanford.edu|access-date=2014-05-02}}</ref> <syntaxhighlight lang="pascal"> X := Y XOR X; // XOR the values and store the result in X Y := X XOR Y; // XOR the values and store the result in Y X := Y XOR X; // XOR the values and store the result in X </syntaxhighlight> Since XOR is a [[commutative operation]], either X XOR Y or Y XOR X can be used interchangeably in any of the foregoing three lines. Note that on some architectures the first operand of the XOR instruction specifies the target location at which the result of the operation is stored, preventing this interchangeability. The algorithm typically corresponds to three [[machine code|machine-code]] instructions, represented by corresponding pseudocode and assembly instructions in the three rows of the following table: {| class="wikitable" style="width: 45em;" |- ! Pseudocode !! IBM [[System/370]] assembly !! x86 assembly !! RISC-V assembly |- | {{code|1=X := X XOR Y|2=pascal}} || {{code|1=XR R1,R2|2=asm}} || {{code|1=xor eax, ebx|2=asm}} || {{code|1=xor x10, x11 |2=asm}} |- | {{code|1=Y := Y XOR X|2=pascal}} || {{code|1=XR R2,R1|2=asm}} || {{code|1=xor ebx, eax|2=asm}} || {{code|1=xor x11, x10 |2=asm}} |- | {{code|1=X := X XOR Y|2=pascal}} || {{code|1=XR R1,R2|2=asm}} || {{code|1=xor eax, ebx|2=asm}} || {{code|1=xor x10, x11 |2=asm}} |} In the above System/370 assembly code sample, R1 and R2 are distinct [[processor register|register]]s, and each {{code|XR|2=asm}} operation leaves its result in the register named in the first argument. Using x86 assembly, values X and Y are in registers eax and ebx (respectively), and {{code|xor|2=asm}} places the result of the operation in the first register. In RISC-V assembly, value X and Y are in registers X10 and X11, and {{code|xor|2=asm}} places the result of the operation in the first register (same as X86) However, in the pseudocode or high-level language version or implementation, the algorithm fails if ''x'' and ''y'' use the same storage location, since the value stored in that location will be zeroed out by the first XOR instruction, and then remain zero; it will not be "swapped with itself". 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 location, in which case their values must already be equal. That is, if ''x'' and ''y'' use the same storage location, then the line: <syntaxhighlight lang="pascal"> X := X XOR Y </syntaxhighlight> sets ''x'' to zero (because ''x'' = ''y'' so X XOR Y is zero) ''and'' sets ''y'' to zero (since it uses the same storage location), causing ''x'' and ''y'' to lose their original values.
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