Reversing High and Low order bits

JSDL

Senior Member
Does anyone have a simple way to swap the high 4 order bits with the low 4 order bits in a byte? For example:

%00110010 becomes %00100011

I've tried various ways using shift right by division with temp variables, etc. but none seem to work.
 

BESQUEUT

Senior Member
Does anyone have a simple way to swap the high 4 order bits with the low 4 order bits in a byte? For example:

%00110010 becomes %00100011

I've tried various ways using shift right by division with temp variables, etc. but none seem to work.
Code:
[color=Purple]b0[/color][color=DarkCyan]=[/color][color=Navy]%00110010[/color]
[color=Blue]sertxd ([/color][color=Black]#[/color][color=Purple]bit7[/color][color=Black],#[/color][color=Purple]bit6[/color][color=Black],#[/color][color=Purple]bit5[/color][color=Black],#[/color][color=Purple]bit4[/color][color=Black],#[/color][color=Purple]bit3[/color][color=Black],#[/color][color=Purple]bit2[/color][color=Black],#[/color][color=Purple]bit1[/color][color=Black],#[/color][color=Purple]bit0[/color][color=Black],[/color][color=Red]"==>"[/color][color=Blue])[/color]
[color=Purple]b1[/color][color=DarkCyan]=[/color][color=Purple]b0
bit0[/color][color=DarkCyan]=[/color][color=Purple]bit4
bit1[/color][color=DarkCyan]=[/color][color=Purple]bit5
bit2[/color][color=DarkCyan]=[/color][color=Purple]bit6
bit3[/color][color=DarkCyan]=[/color][color=Purple]bit7
bit4[/color][color=DarkCyan]=[/color][color=Purple]bit8
bit5[/color][color=DarkCyan]=[/color][color=Purple]bit9
bit6[/color][color=DarkCyan]=[/color][color=Purple]bit10
bit7[/color][color=DarkCyan]=[/color][color=Purple]bit11[/color]
[color=Blue]sertxd ([/color][color=Black]#[/color][color=Purple]bit7[/color][color=Black],#[/color][color=Purple]bit6[/color][color=Black],#[/color][color=Purple]bit5[/color][color=Black],#[/color][color=Purple]bit4[/color][color=Black],#[/color][color=Purple]bit3[/color][color=Black],#[/color][color=Purple]bit2[/color][color=Black],#[/color][color=Purple]bit1[/color][color=Black],#[/color][color=Purple]bit0[/color][color=Black],[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])[/color]
Work with simulator...
 

AllyCat

Senior Member
Hi,

I'm not sure if it can be done in a "one liner" (i.e. one program line and one byte) but you could use:

Code:
w0 = b0 * 16    ; b0 = b0 * 16 , b1 = b0 / 16
b0 = b0 + b1     ; Or could use OR function
Cheers, Alan.
 

BESQUEUT

Senior Member
Does anyone have a simple way to swap the high 4 order bits with the low 4 order bits in a byte? For example:

%00110010 becomes %00100011

I've tried various ways using shift right by division with temp variables, etc. but none seem to work.
Code:
[color=Purple]b0[/color][color=DarkCyan]=[/color][color=Navy]%00110010[/color]
[color=Blue]sertxd ([/color][color=Black]#[/color][color=Purple]bit7[/color][color=Black],#[/color][color=Purple]bit6[/color][color=Black],#[/color][color=Purple]bit5[/color][color=Black],#[/color][color=Purple]bit4[/color][color=Black],#[/color][color=Purple]bit3[/color][color=Black],#[/color][color=Purple]bit2[/color][color=Black],#[/color][color=Purple]bit1[/color][color=Black],#[/color][color=Purple]bit0[/color][color=Black],[/color][color=Red]"-->"[/color][color=Blue])[/color]
[color=Purple]b1[/color][color=DarkCyan]=[/color][color=Purple]b0[/color][color=DarkCyan]/[/color][color=Navy]16[/color]
[color=Purple]b0[/color][color=DarkCyan]=[/color][color=Purple]b0[/color][color=DarkCyan]*[/color][color=Navy]16[/color][color=DarkCyan]+[/color][color=Purple]b1[/color]
[color=Blue]sertxd ([/color][color=Black]#[/color][color=Purple]bit7[/color][color=Black],#[/color][color=Purple]bit6[/color][color=Black],#[/color][color=Purple]bit5[/color][color=Black],#[/color][color=Purple]bit4[/color][color=Black],#[/color][color=Purple]bit3[/color][color=Black],#[/color][color=Purple]bit2[/color][color=Black],#[/color][color=Purple]bit1[/color][color=Black],#[/color][color=Purple]bit0[/color][color=Black],[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])[/color]
 

hippy

Technical Support
Staff member
Either of these will work ...

For M2 : b1 = b0 * 256 | b0 / 16

Fox X2 : b1 = b0 << 8 | b0 >> 4
 

AllyCat

Senior Member
Hi,

b1 = b0 * 256 | b0 / 16
Ah yes, I thought it should be possible; saves a (temporary) RAM byte but uses one or two more program bytes.

Now, if we had the (long-promised) in-line assembler facility, then it wouild be just SWAPF byte,d . ;)

Cheers, Alan.
 

BESQUEUT

Senior Member
saves a (temporary) RAM byte but uses one or two more program bytes..
??? save nothing : b0 and b1 are used !
Same size as #3

You have to write
b0 = b0 * 256 | b0 / 16
if you want to save that RAM byte... and a program byte !

b0 = b0 * 256 + b0 / 16
is the same thing


but (as usual) Hippy is extraordinary clever...
I had to scratch my head at least 10mn to understand how this work...
 
Last edited:

AllyCat

Senior Member
Hi,

Hmm, for me, hippy's version always reports as 1 or 2 program bytes larger than #3 (whether using + or |) and is probably marginally slower; not surprising as it uses one more operator (and a larger constant). However, exact program size depends on the PICaxe chosen (e.g. 08M2 uses less than 20M2) and inclusion of #no_end, etc..

I don't see how any of the proposals that don't use a shifting operator ( << , >> , * , / and sometimes +) can be expected to work. :confused:

Another possibility is:

b0 = bit3 * 2 + bit2 * 2 + bit1 * 2 + bit0 * 2 + bit7 * 2 + bit6 * 2 + bit5 * 2 + bit4

Not sensible in this case, but it's a generic structure for any arbitrary exchange of bits, such as "mirroring" a full byte.

Cheers, Alan.
 

JSDL

Senior Member
once again there is a wealth of knowledge being shared in this forum and as always it has helped me solve the problem. Thank you to everyone for your help!
 

westaust55

Moderator
If using an x1 or X2 part and option is the rev command.

Untested (still away from my PCs)
B0 = b0 rev 4 : b0 = b0 rev 8 : b0 = b0 rev 4

As unary operators may not be able to combine into one line.
 
Last edited:

inglewoodpete

Senior Member
If using an x1 or X2 part and option is the rev command.

Untested (still away from my PCs)
B0 = b0 rev 4 : b0 = b0 rev 8 : b0 = b0 rev 4

As unary operators may not be able to combine into one line.
Yep, that produces the result on an X2-series PICAXE (12 bytes of program space).

As noted, "Rev" is not available on an M2.
 
Top