Petit Computer has three bitwise operators which take two operands each, AND
, OR
and XOR
. When given numerical values, these values are rounded down to the nearest integer before the bitwise operations are performed.
It is important to note that these are implemented as bitwise operations, and that they behave identically to the logical operations when the operands are TRUE and FALSE, but do not behave the same as logical operators when other numerical values are used.
For instance:
X=3 IF X THEN PRINT "X is 'true'" 'prints Y=4 IF Y THEN PRINT "Y is 'true'" 'prints IF TRUE AND TRUE THEN PRINT "TRUE AND TRUE is 'true'" 'prints IF X AND Y THEN PRINT "X AND Y is 'true'" 'does not print
Note that if X is 5 and Y is 3, for instance, the last line will print.
In a similar way, the value 0.5 when used as a boolean value will have the sense TRUE because it is nonzero, but if it is used as an operand to AND, OR, or XOR, it will be rounded down to 0, which is FALSE
.
Because of the rounding, and because ORing with zero does not change a value, the code (0OR(expression))
can be used instead of FLOOR(expression)
. Use of FLOOR
is recommended, though, for two reasons: it makes the intention of the operation explicit, and it's slightly faster. However, there are circumstances where the outer parentheses of (0OR(expression))
can be omitted, and there are circumstances where the inner parentheses can be omitted (though a space character may be necessary after the OR
keyword), which can save a few characters of program memory - so, if reducing the number of characters used in program memory is important, 0OR
may sometimes be useful as an alternative to FLOOR
.
The bitwise operators have the lowest precedence of all operators. This means that every other type of operator (multiplication, addition, the relational operators, etc.) gets evaluated before these ones. So, X1==X0 AND B
gets evaluated as (X1==X0) AND B
. The operators are left-associative, so when the bitwise operators do get evaluated, the leftmost ones are evaluated first. A AND B OR C
is evaluated as (A AND B) OR C
.