Rapid-Q Documentation by William Yu (c)1999 Appendix C: Operators


Introduction to Operators

Operators perform mathematical or logical operations on values. They are usually encompassed by an expression. For example, 2 * 8 is a valid expression, and * is an operator operating on values 2 and 8. Rapid-Q can evaluate both INFIX and POSTFIX (RPN) expressions, but if you want to evaluate POSTFIX expressions, you'll need to note the special cases.

Arithmetic operators
Rapid-Q maintains a table of precedence for each operator. The highest precedence operator will always execute before any lower precedence operator. For operators that share the same precedence, the left to right associativity law holds. Expressions enclosed in braces (...) automatically have higher precedence than expressions outside the braces.
Operation Operator Precedence Description
String index [] 1 Returns a character from a string
ie. s$[2]
returns the second character in string s$
Exponentiation ^ 1 Calculates power of a number
ie. 2^6
is 2 to the power of 6
Negation - 1 Negates a number
ie. -99
is negative 99
Multiplication * 2 Multiplies 2 numbers
ie. 2*6
is 2 multiplied by 6
Floating-point Division / 2 Divides 2 floating point numbers
ie. 6.5/2.6
is 6.5 divided by 2.6
Integer Division \ 2 Divides 2 integer numbers
Before integer division is performed, operands are rounded to integers. The result is truncated to an integer value.
ie. 6\2
is 6 divided by 2
Left Bit Shift SHL 2 Shifts bits left by amount specified
ie. 10 SHL 2
is number 10 whose bits are shifted 2 bits to the left
Right Bit Shift SHR 2 Shifts bits right by amount specified
ie. 10 SHR 2
is number 10 whose bits are shifted 2 bits to the right
Modulus/Remainder MOD 3 Returns the remainder of the division
ie. 15 MOD 10
is 15/10 whose remainder is 5
Inverse Modulus INV 3 Returns the inverse of a number in modulus
ie. 3 INV 26
The inverse of 3 is 9 in 1 (MOD 26)
Addition + 4 Add 2 operands (strings included)
ie. 3+6
is 3 plus 6 = 9
ie. "hi"+"world"
is "world" appended to "hi" = "hiworld"
Addition & 4 Same as using '+' but maintained for compatibility with other BASIC languages, such as VB.
Subtraction - 4 Subtracts 2 operands (strings included)
ie. 6-3
is 6 minus 3 = 3
ie. "jello"-"l"
is "jello" minus all occurrences of "l" = "jeo"


Relational operators
Relational operators are used to compare 2 values. The result of this comparison is either "true" (nonzero) or "false" (zero). You may assume that "true" equals -1.
Relation Operator Precedence Description
Equality = 5 Tests for equality between 2 operands (strings included)
ie. 2=2
if 2 equals 2 then "true" else "false"
Inequality <> 5 Tests for inequality between 2 operands (strings included)
ie. "hello"<>"world"
if "hello" is not equal to "world" then "true" else "false"
Less than < 5 Tests if operand is less than another (strings included)
ie. 2<10
if 2 is less than 10 then "true" else "false"
Greater than > 5 Tests if operand is greater than another (strings included)
ie. "z">"a"
if "z" is greater than "a" then "true" else "false"
Less than or equal <= 5 Tests if operand is less than or equal to another (strings included)
ie. 2<=10
if 2 is less than or equal to 10 then "true" else "false"
Greater than or equal >= 5 Tests if operand is greater than or equal to another (strings included)
ie. 20>=10
if 20 is greater than or equal to 10 then "true" else "false"


Logical operators
Logical operators perform tests on multiple relations, bit manipulation, or Boolean operations and return a true (nonzero) or false (zero) value to be used in making a decision.
Operation Operator Precedence Description
Logical complement NOT 6 Returns the complement (inverted bits)
ie. NOT -1
-1 has all bits set, NOT -1 inverts all bits
Conjunction AND 7 Compare corresponding bits in 2 operands and sets the corresponding bit in the result to 1 if both bits are 1.
ie. 5 AND 3
5 AND 3 = 1 since their first bits are set
Disjunction (inclusive "or") OR 8 Compares corresponding bits in 2 operands and sets bit to 1 if either one has a corresponding bit set.
ie. 5 OR 3
5 OR 3 = 7 since bits overlap
Exclusive "or" XOR 9 Compares corresponding bits in 2 operands and sets bit to 1 if only one of the operands has a corresponding bit set.
ie. 5 XOR 3
5 XOR 3 = 6 since one bit overlaps


INFIX/POSTFIX notation
Expressions, in most languages, are expressed in INFIX notation. Rapid-Q prefers INFIX notation, but can also handle POSTFIX notation with a few special cases.
      Example INFIX expression:
          A = 4 * 7 + (4 - 1)^6
INFIX notation is easier to use and understand than POSTFIX notation. In fact, Rapid-Q's POSTFIX notation is a simple side effect, and was not intentional. Please avoid using POSTFIX if possible.
      Example POSTFIX expression:
          A = (4) (7) (*) (4) (1) (-) (6) (^) (+)
The 2 expressions should evaluate to 757. As you'll note, when dealing with POSTFIX notation, make sure all operands and operators are enclosed in braces. When dealing with negation, you'll have to do this instead:
      Example POSTFIX expression w/negation:
          A = (5) (0-5) (-)
Notice that (0-5) and (-5) return different results.


Contents Internal Definitions ->