Shop OBEX P1 Docs P2 Docs Learn Events
Testing for a particular bit in a byte value — Parallax Forums

Testing for a particular bit in a byte value

DavidMDavidM Posts: 630
edited 2008-02-28 22:56 in Propeller 1
Hi,

How do I test for particular "BIT" of a value is set to 1 or 0??

e.g. I have a 4 bit value ..

Value := $0111

I want to test if a particular bit of any of its 4 bits) , to see if its a 1 or a 0

Lets say I want to check the 3rd bit ( starting from the left )

How do I do this? Do I use BITWISE DECODE?? or maybe bitwise AND?

Thanks


Dave M

Comments

  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-02-28 10:28
    First of all do a shift left and then do a bitwise and with one. This will give you a 1 or a 0 which can be use as true/false. In most things in SPIN you can just use 0 for false and anything else for true.
  • DavidMDavidM Posts: 630
    edited 2008-02-28 10:41
    Hi,

    Would this be what I need to do ( I don't see why I would have to shift left at all)

    e.g. To test the first ( Leftmost) bit

    MyValue := %1010
    IF MyValue & %1000 == %1000

    This if statement above would test TRUE as the first bit is 1


    or to test the 2nd bit from the left

    MyValue := %1010
    IF MyValue & %0100 == %0100

    would return FALSE as the 2nd bit is 0

    etc..

    Regards


    Dave M
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-02-28 10:50
    You could do that as well smile.gif Now that we have the spin source we can tell which is quicker but I bags not doing it(could just use the timer values to figure it out). The advantage with my way is that it should be easier if you want to use the routine for different bits. Also, since I am using 1 I think that there is a spin opcode for pushing 1 onto the stack which will save a couple of bytes and speed things up slightly.
  • DavidMDavidM Posts: 630
    edited 2008-02-28 11:03
    Ok Steve,

    Your idea of shifting left by the number of bits makes sense so you can see what bit you are testing for as well as keeping the code generic.


    Thanks

    Dave M
  • Ron SutcliffeRon Sutcliffe Posts: 420
    edited 2008-02-28 11:10
    @steve
    wouldn't this do,

    IfNot Myvalue>>4
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-02-28 11:14
    Depends on whether there are other bits set. For example if you had
    myValue:=%0100
    and you did
    if myValue>>2

    then it would work but if you had
    myValue:=%1100
    then it wouldn't work.

    You could do

    if (myValue>>2)&1

    You would have to check to see exactly what 'ifnot' checks for as well.
  • CardboardGuruCardboardGuru Posts: 443
    edited 2008-02-28 11:27
    DavidM said...
    MyValue := %1010
    IF MyValue & %1000 == %1000

    This if statement above would test TRUE as the first bit is 1
    Hi David,

    In 99% of cases that's exactly what you do.· Better yet, set up a named constant for the %1000.

    If you did have to be adaptable to check any bit, SPIN has an operator that sets a single bit by number.

    |<0 gives %1
    |<3 gives %1000
    |<31 gives %10000000_00000000_00000000_00000000

    So you could do:

    MyValue := %1010
    IF Myvalue &·|<3



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Help to build the Propeller wiki - propeller.wikispaces.com
    Play Defender - Propeller version of the classic game
    Prop Room Robotics - my web store for Roomba spare parts in the UK
  • Ron SutcliffeRon Sutcliffe Posts: 420
    edited 2008-02-28 11:37
    @steve
    So what you first said was :-
    Mybit :=(myvalue>>Mybit)&1

    I think.

    regards

    Ron
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2008-02-28 13:31
    How about this:
    pub testbit(value,bit)
      result := value & |<bit
    
    


    Any nonzero result is true though you could promote it to a true true [noparse]:)[/noparse] with:
      result OR= 0
    
    


    use it like this:
      if testbit(mybits,2)
        do_this_because_its_set
    
    


    The above statement does not need to compare testbit to a true or false because the result of testbit is true or false. If the bit is set then the result is true.

    *Peter*
  • DavidMDavidM Posts: 630
    edited 2008-02-28 22:29
    HI,

    Thanks everyone!


    I think that BITWISE DECODE is the go for this!

    I just have to remember that the first bit ( starting from the RIGHT ) is bit 0 not 1

    |<0 First bit starting from the right
    |<1 2nd bit
    |<2 3rd bit
    |<4 4th bit etc..


    So..

    IF MyValue |<3

    would result in TRUE if the 4th bit from the right is set to 1

    is this correct?


    regards


    Dave M
  • DavidMDavidM Posts: 630
    edited 2008-02-28 22:31
    Sorry I left out the "&"

    that should be..

    IF MyValue & |<3

    would result in TRUE if the 4th bit from the right is set to 1

    Dave M
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-02-28 22:56
    Correct

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

    Parallax, Inc.
Sign In or Register to comment.