Shop OBEX P1 Docs P2 Docs Learn Events
Bit flags revisited — Parallax Forums

Bit flags revisited

James LongJames Long Posts: 1,181
edited 2007-01-01 18:01 in Propeller 1
Ok.....so I remember we had a discussion about bit flags.

I'm loosing my memory...but I don't remember a direct way to set a bit (in a byte) and use the other bits as well.

Like having a whole byte as flags...and being able to set each one for a different event.

As such:
Bit #0 would be for a set point if a temperature had been reached.
Bit #1 would be for a motor had been turned on.

so on and so forth.

I know I could read the byte...then just add a number to it that would equal the bit to be set. But that could really be a nightmare if you got the number wrong.

I'm just asking...to be sure. I don't mind doing it this way....but it takes more instructions to do so.

Thanks for the revisit,

James L

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-01 08:22
    The OR instruction sets one or more bits. The ANDN instruction clears one or more bits. The XOR instruction inverts/toggles one or more bits. The |< operator takes a bit number and converts it to a bit value. With a literal value, you can use bits 0 to 8.
       OR   temp,#|<3   ' Sets bit 3
       ANDN temp,#|<3 ' Clears bit 3
       XOR temp,#|<3  ' Toggles bit 3
    
    


    You can combine several bits in one instruction by adding (+) or using the bit or operator (|).
  • James LongJames Long Posts: 1,181
    edited 2007-01-01 14:36
    Mike,

    Oh....ok...I haven't researched any assembly commands yet.....I'm still working in Spin.

    I understand all of that except the (+) or (-) operator.....I'll take a look in the book.

    You are so much better at assembly than I. I just don't know it. And haven't started to learn it.

    Thanks,

    James L
  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-01 16:08
    James,
    This works the same way in SPIN as in assembly except that there isn't a single operator for ANDN.
       temp |= |<3  ' Sets bit 3
       temp &= !|<3 ' Clears bit 3 (and not)
       temp ^= |<3 ' Toggles bit 3
    
    


    The comment about the (+) ... You can combine separate bit with addition as well as bit or because
    the bit "columns" add without carry as long as you only have one of each bit. In other words, you
    can add or use logical or for %00010101 to %10101000 to get %10111101. On the other hand,
    if you add %101 to %001 you get %110. If you use logical or, %101 | %001 is %101.
  • James LongJames Long Posts: 1,181
    edited 2007-01-01 16:26
    Mike,

    Thanks that is a great help.

    That will make setting individual bits very easy.

    Now that I can set them......I'm scratching my head how to read them.

    considering myvariable.bit[noparse][[/noparse]1] is not allowed.

    Hmmm.....another hurdle......aint this fun!!!

    Thanks again.....you are a great resource.



    James L


    Post Edited (James Long) : 1/1/2007 4:32:22 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-01 17:11
    James,
    In SPIN, use "if temp & |<3" to test for true, "if not temp & |<3" to test for false.

    In assembly, use
               and    temp,#|<3   wz
      if_nz  jmp  #somewhere
    
    


    to jump to somewhere if true. Use "if_z" to jump if false.
  • James LongJames Long Posts: 1,181
    edited 2007-01-01 17:16
    Wow....thanks Mike...

    How about programming it for me too.......burger.gif

    Just kidding....you have helped me alot.

    I have quite a few flags to set...and didn't want to use a full byte for each.



    James L
  • SSteveSSteve Posts: 808
    edited 2007-01-01 17:41
    When you write your code, be sure to use constants for the bit position. "if flags & |< motorOn" conveys a lot more information than "if flags & |<3".

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • James LongJames Long Posts: 1,181
    edited 2007-01-01 18:01
    That is a great idea too...altough I hadn't thought about it....I would have probably done it that way. My code is quite extensive....and it is getting very complex and a lot of it.



    James L
Sign In or Register to comment.