swap byte

hls

New Member
Hi,
is there any command to swap high four bits and low four bits?
example: %11001010 -> %10101100
I found any.
Or some fast trick.
Thank you.
 

hls

New Member
this won't work. cause picaxe maths is performed strictly from L to R and there is no priority for * and /
shifting left 16 is not enough.

hippy is right
 

womai

Senior Member
You are right (just tried it in the simulator). Guess I spent too much time with "real" programming languages that don't touch the variable itself until the calculation is complete... :)
 

inglewoodpete

Senior Member
I understand that the intermediate maths is done in a 16-bit scratchpad area.

Hence the b0 * 256 does not wipe out the value, just moves the byte to the left by 8 bits. Then the original byte is merged back in and the whole 16 bits are than moved 4 bits to the right.

Clever stuff.
 

hippy

Technical Support
Staff member
@ inglewoodpete : You're correct in the 16 bit intermiediate maths and operation of the code.

@ hls : No, there's no command to swap the nibbles.
 

westaust55

Moderator
so, there is no straight command to swap bits, isn't it?
As hippy says, no simpler way than his very neat equation to swap two nybbles in the same byte.

However, if you want to reverse the order of a certain number of bits within a variable then you can use the REV command.
 

AllyCat

Senior Member
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.*
b0 = b0 * 256 | b0 / 16
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.
 

hippy

Technical Support
Staff member
Well actually there is. ;)
b0 = b0 * 257 / 16 ; Swaps the high and low nibbles of any byte
Well spotted. 257 * $ab = $abab, then / 16 & $FF = $ba, the & $FF is implicit in storing to a byte variable.

I guess that if I had used "+" rather than "|" (OR) in my original code this optimisation might have been easier to see.
 

AllyCat

Senior Member
Hi,

Indeed, and more logical for a "Real Programmer" (or at least an Engineer :) ) to think in terms of "* 256" (i.e. "<<8" ) or even "HighByte = LowByte" and then a simple "INC", as being more efficient (as it would be in many computer compilers) than "* 257" .

Cheers, Alan.
 
Top