Printing result:"myByte contains 8"

Code: Select all

```
dim as integer myByte= 6
if myByte and 8 = 8 then
print "myByte contains 8"
else
print "myByte does not contain 8"
end if
```

Is below if statement legal?Thanks in advance.

Printing result:"myByte contains 8"

Printing result:"myByte contains 8"

Code: Select all

```
dim as integer myByte= 6
if myByte and 8 = 8 then
print "myByte contains 8"
else
print "myByte does not contain 8"
end if
```

Probably parenthesis due to C style auto testing integers for 0 and same worth for bitwise and logical AND.

I.e. (mybyte) and (8=8) vs (mybyte and 8 )=8

I.e. (mybyte) and (8=8) vs (mybyte and 8 )=8

Last edited by marcov on Jan 31, 2024 14:58, edited 1 time in total.

In the *Operator Precedence* documentation page, you can see that the `'= (equal)`' operator (do not confuse with the `assignment` operator) has a precedence higher than the `'And'` operator.

So:

`'if myByte and 8 = 8 then'`

is parsed as:

`'if (myByte) and (8 = 8) then'`.

To go against the operator precedence, you must insert parentheses:

`'if (myByte and 8) = 8 then'`

So:

is parsed as:

To go against the operator precedence, you must insert parentheses:

Thank you both.

This is great the language does not require that the parentheses are mandatory in IF statement unless certain situation takes place.

This is great the language does not require that the parentheses are mandatory in IF statement unless certain situation takes place.

You can also replace "and" with "AndAlso" (that is more appropriate, in such a situation, actually)

Original BASIC used bitwise AND as logical AND, as well, and it didn't make a real difference because it was supposed to work mostly with variable comparisons. But when used with function results, using logical or bitwise operator produces different results

Original BASIC used bitwise AND as logical AND, as well, and it didn't make a real difference because it was supposed to work mostly with variable comparisons. But when used with function results, using logical or bitwise operator produces different results

Thank you!!angros47 wrote: ↑Feb 02, 2024 18:40 You can also replace "and" with "AndAlso" (that is more appropriate, in such a situation, actually)

Original BASIC used bitwise AND as logical AND, as well, and it didn't make a real difference because it was supposed to work mostly with variable comparisons. But when used with function results, using logical or bitwise operator produces different results