Shop OBEX P1 Docs P2 Docs Learn Events
Bit Test — Parallax Forums

Bit Test

RyoshimaRyoshima Posts: 34
edited 2011-08-17 11:20 in Propeller 1
Is there anyway to do a bit test for a byte in spin?

Comments

  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-08-17 11:12
    Something like this would work.
    IF (val & |< 5)
    
    This will return true if bit 5 is set.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-08-17 11:16
    You have to use the various bit operators. Here's an example:
    CON bitNumber = 6
    VAR byte testValue
    
    PUB checkTheBit
       if testValue & |< bitNumber
          ' do something if the bit is a 1
       ifnot testValue & |< bitNumber
          ' do something if the bit is a 0
    
    Note that this makes use of the Spin convention that a zero value is false and a non-zero value is true.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-08-17 11:20
    I use this little method in some of my projects; it returns 0 or 1
    pub bit_val(value, position)
    
      return (value & (1 << position)) >> position
    
Sign In or Register to comment.