Shop OBEX P1 Docs P2 Docs Learn Events
How to extract a single bit from a byte variable. — Parallax Forums

How to extract a single bit from a byte variable.

tore eilertsentore eilertsen Posts: 15
edited 2007-12-15 18:32 in BASIC Stamp
Hi

Im using the philips PCF8574 as a port expander for a bs2p on i2c.

Question..

the variable returned from the chip, inbyte is defined as byte, how can I scan true the 8 bits of inbyte and
determine which is 0 and which is 1, and take an action if say .. bit 4 is 1

can someone please help out with this problem ???


tore

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-12-15 12:13
    tore -

    The two ways I can think of at the moment are:

    1. Bit masking - Using the AND and OR functions

    2. Use the NCD function

    All of the above can be found in the PBASIC Reference Manual, and the PBASIC Help File.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2007-12-15 13:36
    [color=blue]inbyte  VAR  Byte[/color]
     
    [color=blue]' testing bit 4[/color]
     
    [color=blue]IF inbyte & %00010000 > 0 THEN[/color][color=red] ... [/color]
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-12-15 18:32
    You can also do it in these two ways, which will help with readablity:

    inbyte  VAR Byte
    mybit   VAR inbyte.BIT4
    ...
    IF (mybit) THEN
    
    
    


    OR

    inbyte  VAR Byte
    ...
    IF (inbyte.BIT4) THEN
    
    
    


    -Phil
Sign In or Register to comment.