Shop OBEX P1 Docs P2 Docs Learn Events
How to set / reset bit in the variable? (Spin) — Parallax Forums

How to set / reset bit in the variable? (Spin)

AlarusAlarus Posts: 48
edited 2013-05-29 07:40 in Propeller 1
How to set / reset any bit in the variable?
And how to test a bit in a variable?

Regards Alarus

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2013-05-27 02:19
    v |=  |< bit                  ' set
      v ^=  |< bit                  ' toggle
      v &= !|< bit                  ' reset
    
      if v & |< bit                 ' test
        ' do something
    
  • AlarusAlarus Posts: 48
    edited 2013-05-27 03:44
    kuroneko wrote: »
    v |=  |< bit                  ' set
      v ^=  |< bit                  ' toggle
      v &= !|< bit                  ' reset
    
      if v & |< bit                 ' test
        ' do something
    

    Thank you!
    I wanted to use such construction:
    v |=  $00000001 <- bit                      ' set
    v ^=  $00000001 <- bit                      ' toggle
    v &= $FFFFFFFE <- bit                       ' reset
    
    but your beautiful. :)
  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-05-27 10:43
    I have these methods in my standard template:
    pub rd_bit(target, pos)
    
    '' returns bit value (0..1) of target.pos
    
      if ((pos => 0) and (pos =< 31))
        return (target >> pos) & 1
      else
        return 0
    
    
    pub wr_bit(target, pos, value)
    
    '' writes value.0 to target.pos
    
      if (value & 1)
        return target | (1 << pos)
      else
        return target & !(1 << pos)
    
  • JLockeJLocke Posts: 354
    edited 2013-05-28 06:42
    What's the chances of getting you to post your standard template somewhere? I try to collect the pieces as I see you post these handy routines, but it would be great to get them all in a collection.
    <voice of Roger Rabbit> Pleeeease, Jonny? </Rabbit off>
  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-05-28 09:15
    I'm not sure how useful this will be to you or anyone else, but you're certainly welcome to it.
  • RaymanRayman Posts: 14,665
    edited 2013-05-28 18:45
    This gets asked a lot...

    My personal favorite way is to use the normally unused DIRB, INB, or OUTB registers...
    Just like with INA, you can test and set bits in any of these...
  • AlarusAlarus Posts: 48
    edited 2013-05-29 07:17
    Which of these methods is the fastest?

    Thanks for the idea Rayman.
  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-05-29 07:40
    The good news is that you can use the Propeller to determine how long a snippet of code runs. I do timing tests frequently enough that I made a template just for that purpose.
Sign In or Register to comment.