Shop OBEX P1 Docs P2 Docs Learn Events
Bit manipulation??? — Parallax Forums

Bit manipulation???

James LongJames Long Posts: 1,181
edited 2006-10-24 17:32 in Propeller 1
Ok guys....have a question....not really difficult...but I would like to know.

Is it possible to manipulate one bit within the propeller? Of the variable type.....not on the registers or pins.

Please bear with me....I'm a ladder logic person....not really a written code type.

I would like to use bits as variables/flags....but I don't see a way to use just one bit. Using a byte is just huge overhead.

Thanks,

James L

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2006-10-20 03:52
    All bit manipulation on the propeller is done with 32 bit integer logical operations (&, |, ^, !) including the many different kinds of shift operations. For using individual bits as flags, use a mask like $00400000, preferably as a named constant (for clarity) and use the bit logical operations so to set the bit do "x |= $00400000", to clear the bit do "x &= !$00400000", to test if set do "(x & $00400000) <> 0", and to test if clear do "(x & $00400000) == 0". If you're careful, you can do combinations of bits the same way.
  • cgraceycgracey Posts: 14,133
    edited 2006-10-20 04:17
    If you have over 32··bits, you·could make an array·of longs with routines to read and write by bit number:

    VAR long Bits[noparse][[/noparse]32] '1024 bits

    PRI ReadBit(BitNum) : Bit
    · Bit := Bits[noparse][[/noparse]BitNum >> 5] >> BitNum & 1

    PRI WriteBit(BitNum, State)
    · Bits[noparse][[/noparse]BitNum >> 5] |=·|< BitNum
    · Bits[noparse][[/noparse]BitNum >> 5] ^=·|< BitNum &·not·State·

    If you have only 32 bits, you could use the above code, but get rid of the [noparse][[/noparse]BitNum >> 5] indexes:

    VAR long Bits '32 bits
    ·
    PRI ReadBit(BitNum) : Bit
    · Bit := Bits >> BitNum & 1
    ·
    PRI WriteBit(BitNum, State)
    · Bits |=·|< BitNum
    · Bits ^=·|< BitNum &·not·State·
    ·


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


    Chip Gracey
    Parallax, Inc.
  • James LongJames Long Posts: 1,181
    edited 2006-10-20 16:15
    Mike said...
    All bit manipulation on the propeller is done with 32 bit integer logical operations (&, |, ^, !) including the many different kinds of shift operations. For using individual bits as flags, use a mask like $00400000, preferably as a named constant (for clarity) and use the bit logical operations so to set the bit do "x |= $00400000", to clear the bit do "x &= !$00400000", to test if set do "(x & $00400000) <> 0", and to test if clear do "(x & $00400000) == 0". If you're careful, you can do combinations of bits the same way.
    Mike....the mask has me confused. Setting them.....Ok.....I get that part. I was going to do· combinations.....that way I could have 32 flags in one group.
    Chip said...
    If you have over 32··bits, you·could make an array·of longs with routines to read and write by bit number:

    VAR long Bits[noparse][[/noparse]32] '1024 bits

    PRI ReadBit(BitNum) : Bit
    · Bit := Bits[noparse][[/noparse]BitNum >> 5] >> BitNum & 1

    PRI WriteBit(BitNum, State)
    · Bits[noparse][[/noparse]BitNum >> 5] |=·|< BitNum
    · Bits[noparse][[/noparse]BitNum >> 5] ^=·|< BitNum &·not·State·

    If you have only 32 bits, you could use the above code, but get rid of the [noparse][[/noparse]BitNum >> 5] indexes:

    VAR long Bits '32 bits
    ·
    PRI ReadBit(BitNum) : Bit
    · Bit := Bits >> BitNum & 1
    ·
    PRI WriteBit(BitNum, State)
    · Bits |=·|< BitNum
    · Bits ^=·|< BitNum &·not·State·
    ·
    Chip I know I could have over 32 bit as flags.....but really doubtful. I use them alot ...but not in huge amounts.

    I'll have to read up on your method of affecting them.....but I think I get it.

    James
  • Mike GreenMike Green Posts: 23,101
    edited 2006-10-20 16:28
    James,
    If you'd prefer using a bit number, you can use the "|<" operator. Setting Bit 22 is "x |= |<22". Clearing Bit 22 is "x &= !|<22". Testing Bit 22 to see if it's set is "(x & |<22) <> 0". Toggling Bit 22 is "x ^= |<22" and so on.
    Mike
  • NewzedNewzed Posts: 2,503
    edited 2006-10-20 16:44
    Mike, if you wanted to toggle a bit, couldn't you write:

    x := !|<22

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Don't have VGA?

    Newzed@aol.com
    ·
  • cgraceycgracey Posts: 14,133
    edited 2006-10-20 18:17
    You could even use some innocuous register like VSCL as a bit-addressable flag buffer:

    Bit := VSCL[noparse][[/noparse]BitNum]···· 'read a bit
    VSCL[noparse][[/noparse]BitNum] := Bit···· 'write a bit
    !VSCL[noparse][[/noparse]BitNum]···········'toggle a bit

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


    Chip Gracey
    Parallax, Inc.

    Post Edited (Chip Gracey (Parallax)) : 10/20/2006 6:22:52 PM GMT
  • James LongJames Long Posts: 1,181
    edited 2006-10-20 18:39
    Chip,

    Now that was very helpful.......Thanks.



    Mike,

    Thanks for the suggestion.



    James L
  • Mike GreenMike Green Posts: 23,101
    edited 2006-10-24 16:32
    Sid,
    "x := !|<22" doesn't mean what you think it does. The "!" is a logical "NOT" operator so "|<22" is $00400000 and "!|<22" is $FFBFFFFF, so your statement sets x to $FFBFFFFF.
    Mike
  • cgraceycgracey Posts: 14,133
    edited 2006-10-24 17:32
    Mike,
    You're right. It would need to be:
    x ^= |<22········ 'xor $00400000 into x
    Mike Green said...
    Sid,
    "x := !|<22" doesn't mean what you think it does. The "!" is a logical "NOT" operator so "|<22" is $00400000 and "!|<22" is $FFBFFFFF, so your statement sets x to $FFBFFFFF.
    Mike
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Chip Gracey
    Parallax, Inc.
Sign In or Register to comment.