Shop OBEX P1 Docs P2 Docs Learn Events
Reading Individual Bits in Variables? — Parallax Forums

Reading Individual Bits in Variables?

coryco2coryco2 Posts: 107
edited 2011-04-16 12:51 in Propeller 1
Does anyone know a simple way to test the state of an individual bit in a one-byte variable? I would like to be able to test, for example, whether the third bit in the byte is 1 or 0 regardless of the states of the other bits. Any suggestions?

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2011-04-16 12:02
    Suppose you want to know how this works in SPIN ?!

    PUB CheckBit( value, bitNr )
    return value & |< bitNr

    where value = 0 .. 7 in your case. As parameters in SPIN are LONGs you can also go from 0..31

    Explaination:
    |< is an operator which shifts a 1 bit set at the least significant bit as much to the left as given by the parameter. For example |< 3
    %00000000_00000000_00000000_00000001 shifted left 3 times gives you
    %00000000_00000000_00000000_00001000

    The next operation is a logical and which gives you back all the bits where the bit of both operands are set to one. If one operand only has 1 bit set, the result shows you if this one bit is set in the other operand regardless of all other bits. For example:
    %01111010_11010001_00011001_11011000 &
    %00000000_00000000_00000000_00001000 gives you
    %00000000_00000000_00000000_00001000

    If you use this number in an expression which expects a boolean value, it's the same as TRUE because all values <>0 are TRUE and only 0 is FALSE. So, it's safe to use this function in an IF statement for example.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-04-16 12:10
    I have a bunch of bit-related stuff built into my standard template -- here's the lot:
    pub bitcount(value) | bc
    
    '' Returns # of bits set in value
    
      bc := 0                                                       ' clear count
      repeat 32                                                     ' test all bits
        bc += (value & %1)                                          ' add bit value to count
        value >>= 1                                                 ' next bit
    
      return bc
    
    
    pub bitpos(value, mode) | pos
    
    '' Returns position of 1st bit set
    '' -- mode 0 to scan from lsb, mode 1 to scan from msb 
    '' -- -1 = no bits set
    
      if (value == 0)                                               ' if no bits                                   
        return -1                                                   '  return -1
    
      else
        if (mode == 0)                                              ' check from LSB
          value ><= 32                                              '  flip for >|
          return (32 - >|value)
        else
          return (>|value - 1)
    
    
    pub getnib(target, pos)
    
    '' returns nib value (0..15) of target.nib[pos]
    
      if ((pos => 0) and (pos =< 3))
        return (target >> (pos << 2)) & %1111
      else
        return 0
    
        
    pub getquart(target, pos)
    
    '' returns quarternary value (0..3) of target.quart[pos] 
    
      if ((pos => 0) and (pos =< 7))
        return (target >> (pos << 1)) & %11
      else
        return 0
    
    
    pub getbit(target, pos)
    
    '' returns bit value (0..1) of target.pos
    
      if ((pos => 0) and (pos =< 31))
        return (target >> pos) & 1
      else
        return 0
    
    
    pub putbit(target, pos, value)
    
    '' writes value.0 to target.pos
    
      if (value & 1)
        return target | (1 << pos)
      else
        return target & !(1 << pos)
    
    
    pub togglebit(target, pos)
    
    '' toggles target.pos
    
      return target ^ (1 << pos)
    
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-04-16 12:51
    if byteval & %100 ' Test if the third LSB is nonzero
    ...
Sign In or Register to comment.