Shop OBEX P1 Docs P2 Docs Learn Events
How do you reverse the 2nd lot of 8 bits only! — Parallax Forums

How do you reverse the 2nd lot of 8 bits only!

DavidMDavidM Posts: 626
edited 2007-09-09 09:30 in Propeller 1
HI,

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?

eg

11110000_11110000

needs to be

00001111_11110000


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?

regards

Dave Metus

Comments

  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-09-07 07:22
    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.

    Graham
  • DavidMDavidM Posts: 626
    edited 2007-09-07 07:59
    Thanks Graham,

    I will try that!

    Regards

    Dave M
  • AleAle Posts: 2,363
    edited 2007-09-07 13:27
    If memory is not a problem... a table can help...
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2007-09-07 14:47
    Dave,

    ·
    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

    IC Layout Engineer
    Parallax, Inc.
  • mparkmpark Posts: 1,305
    edited 2007-09-07 14:51
    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 PetersonKen Peterson Posts: 806
    edited 2007-09-07 16:28
    I'm getting dizzy...··· roll.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
  • SkogsgurraSkogsgurra Posts: 231
    edited 2007-09-07 16:50
    We are getting close to the famous "confession" by Unix and C creators http://www.cs.cmu.edu/~jbruce/humor/unix_hoax.html

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • DavidMDavidM Posts: 626
    edited 2007-09-09 09:30
    Thanks everyone for your help, I will try these out soon!

    regards


    Dave M
Sign In or Register to comment.