View Full Version : How do you reverse the 2nd lot of 8 bits only!

09-07-2007, 01:50 PM

how do you just rotate the 2nd lot of 8 bits (bits 8-16 or really bits 7-15 ) and leave the first 8 bits ( 0-7 ) alone?



needs to be


I can reverse the whole thing by using ><16
or I can reverse just the first 8bits by using ><8
but I can't just reverse the 2nd lot of 8 bits! (That is, the 8 bits on the left)

Do I need to split up the "string" of bits into just 8 bit segments and join them together after I manipulate them?


Dave Metus

Graham Stabler
09-07-2007, 02:22 PM
you could extract the bits with a mask (and with the mask), then manipulate them as required (flip and then shift) and then put them back with a mask although to put them back with a mask would probably require yout to remove the origionals with a mask first.


09-07-2007, 02:59 PM
Thanks Graham,

I will try that!


Dave M

09-07-2007, 08:27 PM
If memory is not a problem... a table can help...

Beau Schwabe
09-07-2007, 09:47 PM

N := N & $FF | N >> 8 >< 8 << 8

That should do what you want.
N & $FF preserve the LSB 8-Bits
N >> 8·RIGHT shifts the MSB 8-Bits (temporarily) into the LSB 8-Bits
>< 8 reverses the LSB 8-Bits
<< 8·LEFT shifts the LSB 8-Bits back into the MSB 8-Bits
the | symbol "OR"'s the LSB 8-Bits with the MSB·8-Bits, reconstructing the value of N

Beau Schwabe (mailto:bschwabe@parallax.com)

IC Layout Engineer
Parallax, Inc.

09-07-2007, 09:51 PM
It's too bad the >< operator clears out the bits it doesn't reverse, otherwise you could just do
x := x >< 16 >< 8 >< 16.
Here's a way to do it without shifting and masking, not really recommended unless you want to confuse whoever has to look over your code six months from now.
x := (x >< 8 >< 8) | (x >< 16 >< 8 >< 16)

Ken Peterson
09-07-2007, 11:28 PM
I'm getting dizzy...··· http://forums.parallax.com/images/smilies/roll.gif


The more I know, the more I know I don't know.· Is this what they call Wisdom?

09-07-2007, 11:50 PM
We are getting close to the famous "confession" by Unix and C creators http://www.cs.cmu.edu/~jbruce/humor/unix_hoax.html


09-09-2007, 04:30 PM
Thanks everyone for your help, I will try these out soon!


Dave M