Code archives/Algorithms/Integer swapping

This code has been declared by its author to be Public Domain code.

Download source code

Integer swapping by BlitzSupport2001
Again, got this from someone else -- no idea who, sorry!

The second version came from [a http://www.cfxweb.net/phpbb/viewtopic.php?topic=78&forum=14]here[/a].
a = 1
b = 2

Print a
Print b

Print

a=a Xor b
b=a Xor b
a=a Xor b

Print a
Print b

; And another version...

Print

a = 1
b = 2

Print a
Print b
Print

a = a + b
b = a - b
a = a - b

Print a
Print b

Comments

itsdanreed2010
Pretty clever.


Czar Flavius2010
On modern processors the Xor swap is slower than using a temp variable.


Code Archives Forum