Between Boolean and Bitwise
Dug
Posts: 11
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 & ?
........
........
H:=phsa
if sensM and H>8
· ........
· ........
.......
.......
My question is: Is And O.K. here, or should it be & ?
Comments
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 )
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.
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.