ZX BASIC:Operators

From BorielWiki

Jump to: navigation, search

Contents

Operators

Operators in ZX Basic are:

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 exists in Sinclair BASIC.

Exponentiation

  • ^ (Power). x^y returns x to the power of 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.

Another difference with Sinclair BASIC is that ZX BASIC does calculate powers to negative exponents (e.g. 2^-1 = 0.5) while Sinclair BASIC raises an Invalid Argument Error.

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.

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 OR NOT
Performs the Logical Conjunction and returns TRUE if and only if both arguments are TRUE.


a AND b
a b Result
False False False
False True False
True False False
True True True
Performs the Logical Disjunction and returns TRUE if any of the arguments is TRUE.


a OR b
a b Result
False False False
False True True
True False True
True True True
Performs the Logical Negation and returns TRUE if the arguments is False and vice versa.


NOT a
a Result
False True
True False
Personal tools