Swap bytes without temporary variable ?

Buzby

Senior Member
Hi All,

Somewhere on the web, and in this forum I think, I've seen a method for swapping two bytes without using a 3rd temporary byte.

Plenty searching, but I still can't find it !.

Please help.
 

tarzan

Senior Member
From manual2:

swap
Syntax:
SWAP variable1, variable2
Function:
Swap the values between two variables.
Information:
The swap command simply exchanges values between two variables.
Example:
b1 = 5
b2 = 10
main:
swap b1,b2
debug
pause 1000
goto main
 

Buzby

Senior Member
Hi All,

Poke and peek looks the way I'll go. ( Can't use 'swap' because the PICAXE is not an X part. )

The XOR method looks like it may be another version of the one I was looking for, which I have just found in the forum !.

In post #15 of this thread :

http://www.picaxeforum.co.uk/showthread.php?t=5785

Womai shows how to do it it with maths

b0 = x
b1 = y

' now swap
b0 = b0 + b1 ' variables hold (x+y) and y
b1 = b0 - b1 ' variables hold (x+y) and x
b0 = b0 - b1 ' variables hold y and x

Try it and see the magic at work :)
Thanks,

Buzby
 
Last edited:
Top