Shop OBEX P1 Docs P2 Docs Learn Events
Between Boolean and Bitwise — Parallax Forums

Between Boolean and Bitwise

DugDug Posts: 11
edited 2008-02-28 18:04 in Propeller 1
I have an IF statement where I need to recognize that if a vairable register (sensM)·is HIGH and·the count in the Cog's clock A accumulator exceeded a count of 8, an action is taken. Both conditions must be TRUE for the action to be taken. If either one is not TRUE, then the action is not taken. The two lines of code are listed below:
........
........
H:=phsa
if sensM and H>8
· ........
· ........
.......
.......

My question is: Is And O.K. here, or should it be & ?

Comments

  • hippyhippy Posts: 1,981
    edited 2008-02-28 15:39
    Yes, AND should be what you want to use.

    Your sensM will be deemed false when zero, deemed true when non-zero, and it's a boolean / logical AND you need with two conditionals rather than bit-wise ( although that's not always entirely correct and perhaps not in this case ).

    I haven't checked the operator precedence rules in the manual ( I rarely do no matter what language I use ) and will always parenthesis to achieve exactly what I want even if it may be unnecessary. I'd therefore use -

    if sensM and ( h > 8 )
  • DugDug Posts: 11
    edited 2008-02-28 16:39
    Tnx Hippy.

    Although that line of·code appears to work, I'l add the parenthesis as you suggested to be sure.

    In another location of the program I read four switches to determine the binary code and the action necessary. There I have two IF statement lines and use the & as follows:

    if A & B

    · ifnot C & D

    ··· ........

    I notice using And won't work as I am comparing in pure binary so the bitwise· (&) operator was used and that worked.

    Thanks for the input.
  • hippyhippy Posts: 1,981
    edited 2008-02-28 18:04
    A lot rests upon what values the variables have with respect to how IF works. An IF action occurs when true, when the expression evaluates to non-zero, it is false otherwise. Thus "3 & 1" is non-zero so true, "4 & 1" is zero thus false.

    What's written in "IF expression" is usually a shorthand of what it really means, thus "if A & B" is shorthand for "if ( A & B ) <> 0", "if A and B", shorthand for "if (A<>0) and (B<>0)".

    Sometimes "if A and B" and "if A & B" are interchangeable, sometimes not.
Sign In or Register to comment.