Shop OBEX P1 Docs P2 Docs Learn Events
Rotate bits — Parallax Forums

Rotate bits

aekloaeklo Posts: 20
edited 2006-02-27 21:49 in BASIC Stamp
I've been trying to figure out a simple method of rotating bits in Stamp Basic, but need some help...
Is there a function or simple way of doing a bit rotationof eg. a byte?

Something like:
B1 VAR Byte
B1 = %00000001
rot(B1) 'gives %10000000
rot(B1) 'gives %01000000
rot(B1) 'gives %00100000
etc.

/Anders

Comments

  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-02-27 19:32
    Check out the ">>" operator:
    B1 VAR Byte
    B1 = %00000001
    B1 = B1<< 7
    B1 = B1 >> 1
    B1 = B1 >> 1

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-02-27 20:05
    Keep in mind that the shift instructions drop of the "end" bit, depending on which way you're shifting.· You can create a subroutine that allows you to catch that bit and put it back into the other side:

    Rotate_Left:
    ··DO WHILE (idx > 0)
    ····carry = temp.BIT7
    ··· temp = temp << 1
    ··· temp.BIT0·= carry
    ··· idx = idx - 1
    · LOOP
    · RETURN


    Rotate_Right:
    ··DO WHILE (idx > 0)
    ····carry = temp.BIT0
    ··· temp = temp·>> 1
    ··· temp.BIT7·= carry
    ··· idx = idx - 1
    · LOOP
    · RETURN


    In·either case, temp gets rotated, carry is a bit-variable for·storage, and idx is the number of positions to rotate.
    ·


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • aekloaeklo Posts: 20
    edited 2006-02-27 21:49
    Thanks Jon! That procedure will do smile.gif

    /Anders
Sign In or Register to comment.