Shop OBEX P1 Docs P2 Docs Learn Events
Accessing Individual bits within a byte — Parallax Forums

Accessing Individual bits within a byte

edited 2008-03-15 06:22 in Propeller 1
I am trying to overwrite the four low bits in a byte while leaving the four high bits alone. it used to be, with the basic stamp, we could individually address the bits of a byte with the .bit[noparse][[/noparse]0] command. Since we can no longer do that, does anyone have an idea for a workaround?

Ryan Brandys

Comments

  • OzStampOzStamp Posts: 377
    edited 2008-03-12 02:01
    Hi

    In Spin or ASM just some quick pointers..explore further from there

    Spin
    old : = old & new

    ASM 'setting bits to zero
    and old, new 'new..zero for overwriting to zero just alter the 4 lsb bits..

    ASM 'setting bits to one
    or old,new



    Rgds Ron Mel oz
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-03-12 02:03
    Yeah in spin it would look something like:
    a := a & $F0 | b & $0F
    

    this replaces the lower 4 bits in a with the lower 4 bits in b.

    I see Ron beat me to it, I agree with his advice.

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

    Parallax, Inc.
  • edited 2008-03-14 17:00
    Thanks, guys! I am now doing some really cool specific overwrites of individual bits within a byte. check out my code:

    PUB Scanning( _Pack ) | counter, pos, addme , eraser

    pos := 1

    repeat counter from 0 to 16

    if _Pack == LeftPack
    eraser := %1010_1010_1111_1111
    if pos == 1
    pos := 2
    addme := %0000_0001_0000_0000
    elseif pos == 2
    pos := 3
    addme := %0000_0100_0000_0000
    elseif pos == 3
    pos := 4
    addme := %0001_0000_0000_0000
    elseif pos == 4
    pos := 1
    addme := %0100_0000_0000_0000
    else
    eraser := %1111_1111_1010_1010
    if pos == 1
    pos := 2
    addme := %0000_0000_0000_0001
    elseif pos == 2
    pos := 3
    addme := %0000_0000_0000_0100
    elseif pos == 3
    pos := 4
    addme := %0000_0000_0001_0000
    elseif pos == 4
    pos := 1
    addme := %0000_0000_0100_0000

    BattState := BattState & eraser
    BattState := BattState | addme
    WriteLEDS 'update LED array
    waitcnt( 9_000_000 + cnt)
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2008-03-15 06:22
    Branmuffin Industries,

    If I understand the code that you posted corectly, you might be able to simplify it down to this...

    PUB Scanning( _Pack ) | counter, pos, addme , eraser 
        pos := 0
        repeat counter from 0 to 16 
          if _Pack == LeftPack
             eraser := %1010_1010_1111_1111 
             addme := %0000_0001_0000_0000
          else
             eraser := %1111_1111_1010_1010 
             addme := %0000_0000_0000_0001 
          addme <<= (pos*2)
          pos += 1
          pos &= %11
          BattState &= eraser
          BattState |= addme 
          WriteLEDS 'update LED array
          waitcnt( 9_000_000 + cnt)
    [url=http://forums.parallax.com/member.php?u=49994][/url] 
    

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

    IC Layout Engineer
    Parallax, Inc.
Sign In or Register to comment.