Chapter 3: Operators


Introduction

In complex expressions, operators with higher precedence are evaluated before the operators with lower precedence; operators of equal precedence are evaluated according to their position in the expression starting from the left.

Operator Priority
not first (highest)
*, div, mod, and, <<, >> second
+, -, or, xor third
=, <>, <, >, <=, >= fourth (lowest)

3.1 Arithmetic Operators

Overview of arithmetic operators in BASIC:

Operator Operation Operand types Result type
+ addition byte, short, integer, words, longint byte, short, integer, words, longint
- subtraction byte, short, integer, words, longint byte, short, integer, words, longint
* multiplication byte, short, integer, words integer, words, long
div division byte, short, integer, words byte, short, integer, words
mod remainder byte, short, integer, words byte, short, integer, words

A div B is the value of A divided by B rounded down to the nearest integer. The mod operator returns the remainder obtained by dividing its operands. In other words,

X mod Y = X - (X div Y) * Y.

If 0 (zero) is used explicitly as the second operand (i.e. X div 0), compiler will report an error and will not generate code. But in case of implicit division by zero : X div Y , where Y is 0 (zero), result will be the maximum value for the appropriate type (for example, if X and Y are words, the result will be $FFFF).

If number is converted from less complex to more complex data type, upper bytes are filled with zeros. If number is converted from more complex to less complex data type, data is simply truncated (upper bytes are lost).

If number is converted from less complex to more complex data type, upper bytes are filled with ones if sign bit equals 1 (number is negative). Upper bytes are filled with zeros if sign bit equals 0 (number is positive). If number is converted from more complex to less complex data type, data is simply truncated (upper bytes are lost).

BASIC also has two unary arithmetic operators:

Operator Operation Operand types Result type
+ (unary) sign identity short, integer, longint short, integer, longint
- (unary) sign negation short, integer, longint short, integer, longint

Unary arithmetic operators can be used to change sign of variables:

a = 3
b = -a
 ' assign value -3 to b

3.2 Boolean Operators

Boolean operators are not true operators, because there is no boolean data type defined in BASIC. These operators conform to standard Boolean logic. They cannot be used with any data type, but only to build complex conditional expression.

Operator Operation
not negation
and conjunction
or disjunction

For example:

if (astr > 10) and (astr < 20) then
PORTB = 0xFF
end if

3.3 Logical (Bitwise) Operators

Overview of logical operators in BASIC:

Operator Operation Operand types Result type
not bitwise negation byte, word, short, integer, long byte, word, short, integer, long
and bitwise and byte, word, short, integer, long byte, word, short, integer, long
or bitwise or byte, word, short, integer, long byte, word, short, integer, long
xor bitwise xor byte, word, short, integer, long byte, word, short, integer, long
<< bit shift left byte, word, short, integer, long byte, word, short, integer, long
>> bit shift right byte, word, short, integer, long byte, word, short, integer, long

<< : shift left the operand for a number of bit places specified in the right operand (must be positive and less then 255).

>> : shift right the operand for a number of bit places specified in the right operand (must be positive and less then 255).

For example, if you need to extract the higher byte, you can do it like this:

dim temp as word

main:
  TRISA = word(temp >> 8)
end.

3.4 Relation Operators (Comparison Operators)

Relation operators (Comparison operators) are commonly used in conditional and loop statements for controlling the program flow. Overview of relation operators in BASIC:

Operator Operation Operand types Result type
= equality All simple types True or False
<> inequality All simple types True or False
< less-than All simple types True or False
> greater-than All simple types True or False
<= less-than-or-equal-to All simple types True or False
>= greater-than-or-equal-to All simple types True or False