Operators

Operators in ZX Basic can be arithmetical, logical and bitwise ones. For bitwise operators checks the Bitwise Operators page.

Arithmetic Operators

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, Subtraction)
    Operator + (addition) can also be used with strings to perform string concatenation.

  • *, /, mod (Multiplication, Division and Modulo)

Note: mod operator returns the modulus (the reminder) of x / y.
E.g. 12 mod 5 = 2. It does not exist in Sinclair BASIC.

Exponentiation

  • ^ (Power). x^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.

Logical Operators

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

AND

Performs the Logical Conjunction and returns TRUE if and only if both arguments are TRUE.

a b result
False False False
False True False
True False False
True True True
---

OR

Performs the Logical Disjunction and returns TRUE if any of the arguments is TRUE.

a b result
False False False
False True True
True False True
True True True
---

XOR

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.

a b result
False False False
False True True
True False True
True True False
---

NOT

Performs the Logical Negation and returns TRUE if the arguments is False and vice versa.

a result
False True
True False