Table of Contents
Appendix D. Operators and Special Characters
Like algebraic operators, programming language operators perform common operations on one or more operands. Each Operator is one of a group such as arithmetic, logical, bitwise and address operators.
Arithmetic operators are the kind usually encountered in algebra. They combine numeric operands and produce a numeric result.
Bitwise operators combine numeric integer operands and produce a numeric integer result, but operate on a bit-by-bit basis, without carry/borrow propagation from bit to bit.
Logical operators combine numeric or string operands, and produce a logical result. A false result will always give a value of zero. Conventionally, a true result will give a value of either 1 or -1 depending on the particular language. In LynPlex the value of a true result is defined only as 'not zero'. It should not be taken to be either 1 or -1 or any other specific value.
Unary operators operate on a single data object or expression normally to the right of the unary operator. The most common example is the minus sign (-), which is used to indicate negative values, as in -x or -ABS(x+y).
Unary operators have the highest precedence, so unary operators are always executed before adjacent binary operators. Adjacent unary operators execute from right to left.
Binary operators combine two operands into a single value. When the operands are of different data type, where possible the operand with the least restrictive type will be converted to the type of the other operand. If a conversion is not possible the translator will fail with an error.
Note that the term 'binary' also refers to the internal representation of numbers within the computer. The two uses are quite distinct and should not be confused.
Each operator has a precedence, and each is a member of a class that determines its valid operand types, type of result, and conversions rules. Operators with higher precedence are executed before adjacent operators, even when they appear later in an expression.
In a+b*c, the b*c is performed first, then added to a. Precedence limits the need for parentheses to group sub-expressions.
Parentheses can make natural execution order more visible, and override natural execution order when desired. For example, a+(b*c) operates the same as the previous example, while (a+b)*c forces a+b to occur first, the result of which is then multiplied by c.
The following operators are used. They are listed in priority order. Those that are in a group that is higher in the list take precedence over those in a group that is lower down the list.
- ( )
- Parentheses are used to change the normal order of evaluation. An expression enclosed in parentheses is evaluate before anything outside the parentheses.
- Unary Signs
- +, -
- Unary plus and minus signs.
- NOT Operations
- bitwise NOT.
- NOT or !
- Logical NOT (NOT false = true).
See also Special Characters.
- Multiplication/Division Operations
- *, /, \
- Multiplication, Division, Integer Division
- % or MOD
- Remainder (equivalent to a=int(a), b=int(b), a-b*(a/b))
- Modulus (a%b+b*(sgn(a)<>sgn(b)))
- Addition/Subtraction Operations
- +, -
- Addition/Concatenation, Subtraction
- Logical Operations
- <> or !=
- Not Equal
- <, >
- Less Than, Greater Than
- <=, >=
- Less Than or Equal, Greater than or Equal
- belongs to … (see The operator IN)
- Regular expression match (see The operator LIKE)
- Logical AND Operations
- AND or &&
- Logical AND (A AND B)
- Logical OR Operations
- OR or ||
- Logical OR (A OR B)
- Binary Operations
- see 6.04 Binary & Logical Operations
- <<, >>
- bitwise shift left and shift right
- BAND or &
- bitwise AND (A AND B)
- BOR or |
- bitwise OR (A OR B)
- bitwise EQV/XNOR (NOT (A XOR B)).
- bitwise IMP (NOT (A AND NOT B)).otherwise result is 1. |
- bitwise XOR (A XOR B).
- bitwise NAND (NOT (A AND B))
- bitwise NOR (NOT (A OR B))
- : or space
- &h or 0x
- Prefix for hexadecimal constant (0x1F, &h3C)
- &o or 0o
- Prefix for octal constant (0o33, &o33)
- &b or 0b
- Prefix for binary constant (0b1010, &b1110)
- Array definition (see 5.02 Arrays)
- Increase a value by 1 (ix = ix + 1) (see 7.01 Arithmetic Operations)
- Decrease a value by 1 (ix = ix - 1) (see 7.01 Arithmetic Operations)
- Perform an operation on a variable and then store the result back in the variable.
(ix = ix # expression) where # can be any of the arithmetic or logical operators -+/\*%&| or the power operator **.
- Separates commands typed on the same line
- Join code lines (if it is the last character of the line). The result line must not exceed the maximum line size.
See also Continuation.
- Meta-command (if it is the first character of the line) or prefix for file handle.
- The reference indicator. The 'at' symbol is used to identify an argument passed by reference in a procedure or function.
- The escape character. It can be used to prefix a symbol to identify it as a variable name. It can also be used within data streams sent to the console.
- Escaped string indicator. Normally escape characters within a string are treated as text . When a literal string is prefixed with the exclamation mark, any escape character sequences found within the string will be processed.
See: String Literals.
- Single line comments
- /* .. */
- Multiple line comments
Operator class determines certain aspects of how operators behave.
Class 1 operators include binary logical operators && , ^^ , ||.
The operands must be integer or floating point variables or expressions. The result is always $$TRUE or $$FALSE.
Class 2 operators include the binary relational operators = , <> , < , ⇐ , >= , > and their equivalents == , != , !>= , !> , !< , !⇐ .
The operands must be integer, floating point, or string. If one operand is a string, so must the other. String characters are compared until a byte differs or the end of one string is reached, so the effect is alphabetic comparison. The result is always $$TRUE or $$FALSE.
Class 3 operators include binary bitwise operators AND , XOR , OR , and symbolic equivalents & , ^ , | .
The operands must be integer variables or expressions. The result is the operand data type.
Class 4 operators include binary arithmetic operators - , * , / , ** .
The operands must be integer or floating point variables or expressions. The result is the data type of the highest type operand.
Class 5 operators include the binary arithmetic operator + and the string concatenate operator + .
The operands must both be integer or floating point variables or expressions, or both be string variables or expressions. The result is the operand data type.
Class 6 operators include the binary arithmetic operators MOD and \.
The operands must be integer or floating point variables or expressions. If either operand is GIANT, the other operand is converted to GIANT and the result is GIANT. Otherwise, if either operand is XLONG, the other operand is converted to XLONG and the result is XLONG. Otherwise, if either operand is ULONG, the other operand is converted to ULONG and the result is ULONG. Otherwise, both operands are converted to SLONG and the result is SLONG. Note that this means that SINGLE and DOUBLE operands are converted to one integer type or another before the operation takes place. Class 6 operators are integer operators.
Class 7 operators include the binary arithmetic and bitwise shift operators «, », «<, »>.
The operands must be integer variables or expressions. The result is the data type of the left operand.
Class 8 operators include the unary arithmetic operators + and -.
The operand must be an integer or floating point expression. The result is the data type of the operand.
Class 9 operators include the unary logical operators ! and !!.
The operand must be an integer or floating point expression. The result is always $$TRUE or $$FALSE .
Class 10 operators include the unary bitwise operators ~ and NOT .
The operand must be an integer variable or expression, or a function name. When applied to GIANT operands, the result data type is GIANT. Otherwise the result data type is XLONG.
Class 11 operators include the unary address operators & and &&.
The operand of & must be a variable, string, composite, whole array, array node, or array data element. The operand of && must be a string, whole array, or string in a string array. The result data type is always XLONG.
The following table is a summary of the characteristics of all operators recognized by the language:
|&||unary||11||any type||address||12||address of data|
|&&||unary||11||any type||address||12||address of handle|
|NOT, ~||unary||10||integer||same type||12||bitwise NOT|
|!||unary||9||numeric||T/F||12||logical not (TRUE if 0, else FALSE)|
|!!||unary||9||numeric||T/F||12||logical test (FALSE if 0, else TRUE)|
|>>>||binary||7||integer||left type||11||arithmetic up shift|
|<<<||binary||7||integer||left type||11||arithmetic down shift|
|<<||binary||7||integer||left type||11||bitwise left shift|
|>>||binary||7||integer||left type||11||bitwise right shift|
|MOD||binary||6||numeric||integer||9||modulus (integer remainder)|
|AND, &||binary||3||integer||high type||7||bitwise AND|
|XOR, ^||binary||3||integer||high type||6||bitwise XOR|
|OR, |||binary||3||integer||high type||6||bitwise OR|
|>, !⇐||binary||2||num str||T/F||5||greater-than|
|>=, !<||binary||2||num str||T/F||5||greater-or-equal|
|⇐, !>||binary||2||num str||T/F||5||less-or-equal|
|<, !>=||binary||2||num str||T/F||5||less-than|
|<>, !=||binary||2||num str||T/F||4||not-equal|
|=, ==||binary||2||num str||T/F||4||equal (also “!<>”)|
|=||binary||num str||right type||1||assignment|
|T/F||T/F always returned as XLONG|
Unary Address Operators
The unary address operators are & and &&.
& returns the memory address of the following variable, array, array node, array data element, or composite element. Applying & to numeric AUTO variables may produce compile-time “Bad Scope” errors because AUTO variables may be assigned space in CPU registers, which do not have addresses. String and composite variables are always located in memory, so applying & to strings and composites is always valid.
The valid forms of & are:
&array-node &token[func, ]
&array-data &token[func, line, element]
&string-array-node &name$[dept, ]
&string-array-data &name$[dept, stationNumber]
&& returns the handle address of the following string variable , composite variable, whole array, or string array element. Numeric variables and components of composite variables do not have handles, so applying && to them produces compile-time errors. Applying && to AUTO strings, arrays, and composites produces compile-time errors because they may be assigned space in CPU registers, which do not have addresses.
The valid forms of && are:
&&string-variable (non-AUTO) &&name$
&&whole-array (non-AUTO) &&token
&&whole-string-array (non-AUTO) &&symbols$
&&string-array-data &&name$[dept, stationNumber]
…(same result as above) &name$[dept, stationNumber,]
Unary Arithmetic Operators
The unary arithmetic operators are + and - .
+, when a unary operator, is the unary positive operator, which performs no operation on the following operand. It makes sign visible and explicit where appropriate for program clarity.
-, when a unary operator, is the unary negative operator which changes the sign of the following operand. The sign bit of the operand value is inverted, or the operand value is subtracted from zero, whichever is appropriate for the operand data type.
Unary Bitwise Operators
The unary bitwise operators are ~ aka NOT.
~ and NOT return the bitwise inversion of the following operand. All bits are flipped, so every zero bit is made a one, and every one bit is made a zero.
Unary Logical Operators
The unary logical operators are ! and !!.
! returns the logical NOT of the following operand. If the value of the operand is zero, ! returns $$TRUE (-1) . If the value is any non-zero value, ! returns $$FALSE (0).
!! returns the logical TEST of the following operand. If the value of the operand is zero, !! returns $$FALSE (0) . If the value is any non-zero value, !! returns $$TRUE (-1).
Binary Shift Operators
The binary shift operators are >>> , <<< , >> , <<.
>>>, the arithmetic shift right operator, shifts the value of the left integer operand n bits to the right, where n is the value of the integer operand following the »> operator. When the left operand is unsigned, all vacated upper bits are filled with zeros. When the left operand is signed, all vacated upper bits are filled with the most significant bit of the original value. »> is explicitly arithmetic.
<<< , the arithmetic shift left operator, shifts the value of the left integer operand n bits to the left, where n is the value of the integer operand to the right of the «< operator. All vacated lower bits are filled with zeros, regardless of data-type. «< is explicitly arithmetic. The data type of the result is the same as the shifted integer; if significant bits are shifted out of the integer, they are lost and no error occurs.
>> , the bitwise shift right operator, shifts the left integer operand n bits to the right, where n is the value of the integer operand to the right of the » operator. All the vacated upper bits are filled with zeros. » is explicitly bitwise.
<< , the bitwise shift left operator, shifts the left integer operand n bits to the left, where n is the value of the integer operand to the right of the « operator. All the vacated lower bits are filled with zero bits. « is explicitly bitwise. The data type of the result is the same as the shifted integer; if significant bits are shifted out of the integer, they are lost and no error occurs.
Binary Arithmetic Operators
The binary arithmetic operators are ** , / , * , - , + , \ , MOD.
** , the raise to power operator, raises the left operand to the power of the right operand. ** replaces the ^ in conventional BASIC because ^ is bitwise XOR.
/ , the floating point divide operator, divides the left operand by the right operand. / is a floating point divide, so if either operand is an integer type, it is converted to DOUBLE before the operation is performed. The result of / is SINGLE if both operands are SINGLE , otherwise it is DOUBLE . / will also divide SCOMPLEX and DCOMPLEX numbers.
NOTE: At least one operand MUST be of type SINGLE or DOUBLE in order to perform a floating point divide. If neither operands are SINGLE or DOUBLE, then an integer divide is performed. For example:
x = 10 y = 20 put x/y, x/single(y), double(x)/y // result shows 0 .5 .5
* , the multiply operator, multiplies the left operand by the right operand. * will also multiply SCOMPLEX and DCOMPLEX numbers.
- , the subtract operator, subtracts the right operand from the left operand. - will also subtract SCOMPLEX and DCOMPLEX numbers.
+ , the add operator, adds the right numeric operand to the left numeric operand. + will also add SCOMPLEX and DCOMPLEX numbers. + will also concatenate strings - see Binary String Operators.
\ , the integer divide operator, divides the left operand by the right operand. \ is an integer divide, so if either operand is a floating point type, it is converted to SLONG before the operation is performed. The result of \ is always SLONG or XLONG or GIANT.
MOD , the integer modulus operator, divides the left operand by the right operand, but returns the remainder left over from the divide instead of the result of the divide. MOD is defined to use an integer divide, so if either operand is a floating point type, it is converted to SLONG before the operation is performed. The result of MOD is always SLONG or XLONG or GIANT.
Binary Bitwise Operators
The binary bitwise operators are & , | , ^ , aka AND , OR , XOR.
AND and & , the bitwise AND operators, AND the left and right operands on a bit by bit basis.
XOR and ^ , the bitsize XOR operators, XOR the left and right operands on a bit by bit basis.
OR and | , the bitwise OR operators, OR the left and right operands on a bit by bit basis.
Binary Logical Operators
The binary logical operators are && , ^^ , ||.
&&, the logical AND operator, returns $$TRUE if both the left and right operand is non-zero; otherwise it returns $$FALSE.
Binary String Operators
The only binary string operator is +.
+ , the string concatenate operator, appends the right string operand to the end of the left string operand. + also adds numeric and complex values; see Binary Arithmetic Operators.
Binary Relational Operators
The binary relational operators are > , >= , ⇐ , < , <> , = , aka !⇐ , !< , !>= , != , == .
> and !⇐ return $$TRUE if the left operand is greater than the right operand; otherwise they return $$FALSE.
>= and !< return $$TRUE if the left operand is greater or equal to the right operand; otherwise they return $$FALSE.
⇐ and !> return $$TRUE if the left operand is less than or equal to the right operand; otherwise they return $$FALSE.
< and !>= return $$TRUE if the left operand is less than the right operand; otherwise they return $$FALSE.
<> and != return $$TRUE if the left operand is not equal to the right operand; otherwise they return $$FALSE.
= and == return $$TRUE if the left operand is equal to the right operand; otherwise they return $$FALSE.
Conventional BASIC does not have logical operators: && ^^ || ! !!
In conventional BASIC, the bitwise operators AND , XOR , OR , NOT are used in place of the corresponding logical operators, sometimes with undesirable results. For example, if x=1 and y=2 , then x AND y returns zero, which is FALSE , while x&&y returns TRUE . Furthermore, x XOR y returns 3 , which is TRUE , while x^^y returns FALSE.
- (Concatenate And Assign)
- (Add And Assign)
- (Subtract And Assign)
- (Multiply And Assign)
- (Divide And Assign)
- (Integer Divide And Assign)
- (Exponentiate And Assign)
- (Modulus And Assign)
- (Conjunction And Assign)
- (Equivalence And Assign)
- (Implication And Assign)
- (Inclusive Disjunction And Assign)
- (Exclusive Disjunction And Assign)
- (Shift Left And Assign)
- (Shift Right And Assign)
- (Integer Divide)
- (Shift Left)
- (Shift Right)
- (Array Index)
- (String Index)
- (Pointer Index)
- (String Concatenation)
- (String Concatenation With Conversion)
- (String Pointer)
- (Not Equal)
- (Less Than)
- (Less Than Or Equal)
- (Greater Than Or Equal)
- (Greater Than)
- (Inclusive Disjunction)
- (Exclusive Disjunction)
Short Circuit Operators
- (Short Circuit Conjunction)
- (Short Circuit Inclusive Disjunction)
- (Argument Stringize)
- (Argument Concatenation)
- (Address Of)
- (Value Of)
- (Variable Pointer)
- (Procedure Pointer)
Type or Class Operators
- (Member Access)
- (Pointer To Member Access)
- Placement New