Shop OBEX P1 Docs P2 Docs Learn Events
Bit and Byte questions. Help needed. — Parallax Forums

Bit and Byte questions. Help needed.

Don MDon M Posts: 1,653
edited 2012-09-10 15:17 in Propeller 1
I have a byte that is comprised of these 8 bits: aabbcccc and want to be able to write to specific bits or pairs of bits.

I know (after learning from Jonny Macs help) that I can read / write to the upper and lower bits by "& $0F" or ">> 4" but I don't understand how I could read / write to the "aa" or "bb" bits.

Thanks.
Don

Comments

  • JonnyMacJonnyMac Posts: 9,194
    edited 2012-09-05 18:17
    Here's how to write to the aa bits -- you should be able to work the rest out from here:
    workbyte := (workbyte & %0011_1111) | ((newbits & %11) << 6)
    
  • Don MDon M Posts: 1,653
    edited 2012-09-05 18:51
    Thanks Jon. I understand it now.
  • BitsBits Posts: 414
    edited 2012-09-05 19:56
    Would this work for your needs, requires a global variable 'Globalvar"?
    [FONT=arial]Pub SetBit(_in)[/FONT]
    [FONT=arial]
    [/FONT]
    [FONT=arial]  Globalvar := (|< _in) | Globalvar
    


    [/FONT]
  • Don MDon M Posts: 1,653
    edited 2012-09-06 04:07
    Bits wrote: »
    Would this work for your needs, requires a global variable 'Globalvar"?
    [FONT=arial]Pub SetBit(_in)[/FONT]
    [FONT=arial]
    [/FONT]
    [FONT=arial]  Globalvar := (|< _in) | Globalvar
    


    [/FONT]

    Bits- I'll look at your suggestion too. Thanks for your help.
  • Don MDon M Posts: 1,653
    edited 2012-09-10 14:44
    I am having trouble grasping how to get an individual bit of a byte. I was looking through the manual at bitwise decode and others but am not sure how best to do this (read- don't understand it).

    Lets say I have byte abcd (a, b, c, d just represent the bit positions in the byte). Below is what I was thinking for a way to test it but was wondering if this is the best / easiest way to get a, b, c or d.
    byte abcd
    
    abcd >> 3    ' to get bit a
    
    (abcd & $8) >> 3   ' another way to get bit a
    
    (abcd & $4) >> 2  ' to get bit b
    
    (abcd & $2) >> 1  ' to get bit c
    
    abcd & $1          ' to get bit d
    
    

    Thanks.
    Don
  • Mike GMike G Posts: 2,702
    edited 2012-09-10 14:53
    Try this
    CON
      _clkmode = xtal1 + pll16x     
      _xinfreq = 5_000_000
    
      CR            = $0D
      LF            = $0A
      NULL          = $00
    
    DAT
      data  byte  %10101011
    VAR
    
    
    OBJ
      pst           : "Parallax Serial Terminal"
      
    PUB Main | i
      pst.Start(115_200)
      pause(500)
    
      repeat i from 0 to 7
        pst.str(string("Bit "))
        pst.dec(i)
        pst.str(string(" = "))
        pst.dec(GetBit(data, i))
        pst.char(CR)
    
      return
    
    PUB GetBit(value, position)
      if (value & |< position)
        return 1
      else
        return 0
    
    PRI pause(Duration)  
      waitcnt(((clkfreq / 1_000 * Duration - 3932) #> 381) + cnt)
      return
    
  • JonnyMacJonnyMac Posts: 9,194
    edited 2012-09-10 15:09
    I am having trouble grasping how to get an individual bit of a byte.

    I suspect you're making it harder than it is. This will return 1 or 0 from value.pos.
    pub get_bit(value, pos)
    
      return (value >> pos) & 1
    


    While we're at it, here are complimentary methods, set (to 1) and clear (to 0):
    pub set_bit(value, pos)
    
      return (value | (1 << pos))
    
    
    pos clear_bit(value, pos)
    
      return (value & !(1 << pos))
    
  • Don MDon M Posts: 1,653
    edited 2012-09-10 15:17
    Thanks Mike & Jon.
Sign In or Register to comment.