Shop OBEX P1 Docs P2 Docs Learn Events
Setting a single bit in Spin — Parallax Forums

Setting a single bit in Spin

RumpleRumple Posts: 38
edited 2013-02-16 12:19 in Propeller 1
I can't believe I've never tried to do this before. What's the best way to set and reset a single bit of a long in Spin? I've always assumed it was "someregister[0]~~ ",since "outa[0]~~" works, but apparently that's not it.

Something with "|<" in it maybe?

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-02-14 09:47
    This will set a single bit:
    singleBitMask := |< pinNumber
    


    To "or" one mask to another (oring enableMask on to activeMask)
    activeMask |= enableMask
    


    To turn off these same bits (same function as "andn" in PASM):
    activeMask &= !enableMask  
    
    


    Thanks to my fellow forum members for this useful stuff.
  • David CarrierDavid Carrier Posts: 294
    edited 2013-02-14 11:05
    To set a bit use this:
    variable |= |< bit
    
    It will decode the bit, which creates a value with only that bit high, then OR the result into the variable.

    To clear a bit use this:
    variable &= ! |< bit
    
    It will decode the bit, which creates a value with only that bit high, invert it, which makes all bits high except the one you are clearing, then AND the result into the variable, effectively clearing that bit within the variable.

    — David Carrier
    Parallax Inc.
  • RaymanRayman Posts: 14,826
    edited 2013-02-14 11:58
    I like to use the unused INB, OUTB and DIRB registers for this...

    Just copy the long you want to work with to DIRB and then Spin will let you do dirb[SomeBit]:=1 and things like that...
  • RumpleRumple Posts: 38
    edited 2013-02-14 15:13
    Nailed it. Thanks for your help guys.

    Also, that's brilliant, Rayman.
  • HarpritHarprit Posts: 539
    edited 2013-02-16 06:00
    How about reading one bit,or a range with compact code
    H
  • JonnyMacJonnyMac Posts: 9,191
    edited 2013-02-16 07:48
    How about reading one bit,or a range with compact code
    bitval := (testval >> bitpos) & %1
    


    If you want to read a group, set bitpos to the LSB value and change the & %1 to something that fits your group; for a nibble it would be & %1111.
  • RaymanRayman Posts: 14,826
    edited 2013-02-16 10:12
    reading a range is really easy using, e.g., INB too...
    For example, you can set bits 4 through 9 to a value like this: INB[4..9]:=value
    You can even set them backwards like this: INB[9..4]:=value
  • HarpritHarprit Posts: 539
    edited 2013-02-16 12:19
    Thanks guys
    I am just finishing up the beginners book and I can add this great stuff to it.
    Just in time.
    H
Sign In or Register to comment.