Operators in ZX Basic ranges from arithmetical and logical to bitwise ones.
Arithmetic operators are left associative (like in Sinclair BASIC) and have the same precedence. The following are a list of arithmetic operators (lower precedence operators appears first):
+(addition) can also be used with strings to perform string concatenation.
mod(Multiplication, Division and Modulo)
modoperator returns the modulus (the reminder) of x / y.
mod5 = 2. It does not exist in Sinclair BASIC.
^y returns x y
Note: Unlike Sinclair Basic, this operator is right associative.
This is the usual behavior in mathematics. So in ZX BASIC:
2^3^2 = 2^(3^2) = 512
(notice the right associative parenthesis), whilst in Sinclair BASIC,
2^3^2 = (2^3)^2 = 64
which is wrong. If in doubt, use always parenthesis to enforce the desired evaluation order.
Logicals operators are like in ZX Spectrum Basic. Their result can be either False (which is represented with 0)
or True, which might be any other value. Don't expect True value number to be always 1.
If you need 0/1 values for boolean evaluations, use
--strict-boolean compiler option.
This might add a little overhead to boolean evaluations, tough.
Operator arguments must be numbers and the result is an unsigned byte value. For binary operators, if arguments are of different types they are converted to a common type before being evaluated:
Table of Logical Operators
Performs the Logical Conjunction and returns TRUE if and only if both arguments are TRUE.
Performs the Logical Disjunction and returns TRUE if any of the arguments is TRUE.
Performs a logical XOR and returns TRUE if one of the arguments is true and one of the arguments is false. In essence, returns true if ONLY one of the arguments is true.
Performs the Logical Negation and returns TRUE if the arguments is False and vice versa.