Hi,
A very old thread (with an imperfect title) that I came across while searching to see if the "SWAP bytes using XORs" code snippet had been formally documented on the forum.*
As hippy says, no simpler way than his very neat equation to swap two nybbles in the same byte.
Well actually there is.
Code:
b0 = b0 * 257 / 16 ; Swaps the high and low nibbles of any byte
Saves a Program byte or two and is about 14% faster in (M2) PICaxe Basic.
Note that (like hippy's) it relies on truncating back to 8 bits an internal calculation of 16-bits, so can't be assigned directly to a word variable.
--------------------------
* I know I've used the method before, but eventually gave up searching and "guessed" the answer almost immediately by trial and error: So this is what I was looking for - it's about half the number of Program bytes and twice the speed of the "resident" PICaxe Basic SWAP command (which exchanges two Bytes or two Words) and doesn't need an additional variable.
Code:
; Fast SWAP (i.e. exchange) of two bytes, without using an additional variable.
b1 = b1 xor b2
b2 = b2 xor b1
b1 = b1 xor b2
Now added as a "Finished Project - Code Snippet", so maybe I'll be able to find it more easily next time.
Cheers, Alan.