Shop OBEX P1 Docs P2 Docs Learn Events
Best way to address bits in a byte? — Parallax Forums

Best way to address bits in a byte?

RichardFRichardF Posts: 168
edited 2008-03-29 09:12 in Propeller 1
I need to be able to read and write individual bits within a byte variable in order to keep track of eight switch positions (on/off). This is easy to do in pBasic using the .BITn form, but I don't see an easy way (or frankly any way) to do this in Spin. Would appreciate some suggestions.
Thanks,
Richard

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-28 13:25
    byteVar &= !Mask ' To set a bit off
    byteVar |= Mask ' To set a bit on
    byteVar ^= Mask ' To toggle a bit
    if byteVar & Mask ' To test if a bit is on
    where
    CON Mask = %10000000 ' or something similar
  • RaymanRayman Posts: 14,225
    edited 2008-03-28 13:27
    Usually, I'd do something like

    Mask:=1<<bit

    to make a mask
  • tekochiptekochip Posts: 56
    edited 2008-03-28 13:33
    Here's some examples with functions to do the same:

    CON
      #0, Serve, Dispense, Flash 'Declare some bits
     
    PUB Demo |coils
     
      OnBit(@coils, Serve)
      ToggleBit(@coils, Flash)
     
      if TestBit(@coils, Flash)
        OffBit(@coils, Serve)
    
    PUB OnBit(x, bit)
    {{Turn a bit on}}
      long[noparse][[/noparse]x]|=1<<bit
     
    PUB OffBit(x, bit)
    {{Turn a bit off}}  
      long[noparse][[/noparse]x]&= !(1<<bit)
     
    PUB ToggleBit(x, bit)
    {{Toggle a bit}}
      long[noparse][[/noparse]x]^=1<<bit
     
    PUB TestBit(x, bit)
    {{Return true if a bit is on}}
      return long[noparse][[/noparse]x] & (1<<bit)        
    
  • RichardFRichardF Posts: 168
    edited 2008-03-28 14:41
    Thanks everyone. Makes me appreciate pBasic (smile)
    Richard
  • hippyhippy Posts: 1,981
    edited 2008-03-28 15:02
    For reading bits you could create a method which handles that, "var.bit4" becomes "bit4(var)", and option which is consistent and works for read and write is "GetBit4(@var)" and "SetBit4(@var,bitVal)", something like ...

    PUB GetBit4( varPtr )
      result := GetBitX(4, varPtr )
    PUB GetBitX( bitNum, varPtr )
      result := ( byte[noparse][[/noparse] varPtr ] & |< bitNum ) <> 0
    
    etc
    
    
  • AribaAriba Posts: 2,687
    edited 2008-03-28 15:57
    Another idea is to use the outb and dirb registers as bitaddressable variables (Prop 1 has this registers but no PortB hardware):
    1)
      outb[noparse][[/noparse] 4] := 1
      if outb[noparse][[/noparse] 5] == 1
        outb[noparse][[/noparse] 4] := 0
    2)
      outb[noparse][[/noparse] 4]~~
      if outb[noparse][[/noparse] 5]
        outb[noparse][[/noparse] 4]~
    
    



    1) is better readable and 2) is more optimized.
    You get up to 64 bits with this methode. And if you don't use the timers, also frqa, frqb, phsa, phsb are good for bitaddressing.

    Andy

    Edit: the space after [noparse][[/noparse] is only for the forum software wink.gif

    Post Edited (Ariba) : 3/28/2008 4:02:15 PM GMT
  • RichardFRichardF Posts: 168
    edited 2008-03-28 16:40
    Two more great ideas, thank you.
    Ariba, your use of the b registers is really cool. I have always assumed they were not addressable, since there are no associated pins.
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2008-03-28 17:07
    Ariba,
    ·
    outb is ok to use on the Current Propeller Chip, but it will not be backwards compatible on the next Propeller Chip
    ·
    Instead, if you are not using any or one of the counters, you could use the frqa, frqb, phsa, and phsb registers to do basically the same thing that you are suggesting.
    Stay away from the ctra, and ctrb registers as you could startup one of the counters without intending to if you write to them.
    ·


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • RaymanRayman Posts: 14,225
    edited 2008-03-28 17:43
    Ariba, Beau,

    I like this idea! I rarely use frqa, frqb, phsa, or phsb in the main cog. So, this is a very nice way to access bits!
  • JavalinJavalin Posts: 892
    edited 2008-03-28 19:09
    Chaps,

    Surely:

    if byteVar & Mask ' To test if a bit is on
    where
      'CON Mask = %10000000 ' or something similar
    


    should be
    if byteVar & Mask == Mask ' to test if a bit is on
    
    

    so -·if byteVar is %00001111 & %00001000 would leave %00001000 so surely wouldn't equate to TRUE/FALSE or 1 or 0?

    ??

    James

    Post Edited (Javalin) : 3/28/2008 7:37:55 PM GMT
  • RaymanRayman Posts: 14,225
    edited 2008-03-28 20:10
    I think zero == false and non-zero == true...
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-03-29 02:45
    correct, just like C, false is 0 and anything else is true, so·it is equivalent to·"if (byteVar & Mask)<>0"

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 3/29/2008 2:52:31 AM GMT
  • JavalinJavalin Posts: 892
    edited 2008-03-29 09:12
    ah - ok!

    J
Sign In or Register to comment.