Operators to process and modify variables with
 

Variables, results of commands or functions and string or numeric values can be combined with operators to make up an expression. An expression is simply a calculation which combines some or all of those things mentioned in the previous sentence. It is generally through the use of expressions that variables can be changed or have values set for them.

Here is a list of operators that you can use in Blitz and what operation they perform, in order of precedence. That is what order they will be executed in if there is more than one operator in the same expression; operators with the same precedence will be evaluated in the order they appear in the expression (LHS = left hand side, RHS = right hand side). The higher the precedence the earlier in the calculation the operator will be evaluated. Parenthesis (brackets) can be added to the expression to force parts of it to be calculated first - parenthesis always be evaluated first.

In Blitz, TRUE has a value of -1 (actually, you can use any non-zero value) and FALSE has a value of 0. Some operations are performed on the binary values of expressions (so bit refers to a BInary digiT).

 NOT (Binary/locigal inverse). Precedence 9 (highest).
The NOT operator works only on numeric values and produces the bitwise inverse of whatever is on the RHS and returns it as a result. That means it changes all the bits which are a '0' to a '1' and all the bits which are '1' get changed to a '0'. (Check out the result of this example using the methods shown in the numbers base page.)
Code a.w = 85 ; Assign a value to a variable
b.w = NOT a ; Calculate the inverse and assign to another variable
NPrint a," ",b ; Display the original and inverted number
MouseWait
End
 - (Arithmetic negative, NB not subtraction). Precedence 9 (highest).
The arithmetic negative operator only works on numeric data types and inverts the sign of the value. It returns the negative value of whatever value is on the RHS. Note that this is not the subtraction operator which has a lower precedence.
Code val.w = -69 ; Assign a negative value to a variable
foo.w = -val ; Calculate the arithmetic negative and assign to another variable
NPrint val," ",foo ; Display the original and negated numbers
MouseWait
End
 BitTst (Bit test). Precedence 8.
Tests the bit specified in the expression on the RHS, from the expression on the LHS. If the bit is a '1' it will return true, otherwise it will return false. This only works on numeric variables.
Code bit.w = 2 ; The bit number we want to test
NPrint "Is bit set in 8? ",8 BitTst bit ; Find out if the bit is set in a number
NPrint "Is bit set in 4? ",4 BitTst bit ; Find out if the bit is set in a number
NPrint "Is bit set in 2? ",2 BitTst bit ; Find out if the bit is set in a number
MouseWait
End
 BitSet (Bit set). Precedence 8.
BitSet only works on numeric values and returns the value of the expression on the LHS after setting the bit, specified by the expression on the RHS, to a '1'.
Code NPrint "Value of 32 with bit 4 set = ",32 BitSet 4
MouseWait
End
 BitClr (Bit clear). Precedence 8.
BitClr only works on numeric values and returns the value of the expression on the LHS after clearing the bit specified by the expression on the RHS. Basically just the same as BitSet but clears the bit to a '0'.
Code NPrint "Value of 15 with bit 2 cleared = ",15 BitClr 2
MouseWait
End
 BitChg (Bit change). Precedence 8.
BitChg only works on numeric values and returns the value of the expression on the LHS after inverting the bit specified by the expression on the RHS (so if it was originally a '1' it gets changed to a '0' and if it was a '0' it gets changed to a '1').
Code NPrint "Value of 15 with bit 2 inverted = ",15 BitChg 2
NPrint "Value of 8 with bit 1 inverted = ",8 BitChg 1
MouseWait
End
 ^ (Raised to the power of). Precedence 7.
Works only on numeric values, it returns the value of the expression on the LHS raised to the power of the expression on the RHS. (This calculation involves multiplying the expression on the LHS by itself the number of times specified in the expression on the RHS. In the case of negative values in the RHS expression, it means to divide instead of multiply.)
Code NPrint 2^3
NPrint 10^2
NPrint 3^0
NPrint 4^-1
MouseWait
End
You'll notice some rounding errors in the calculation of the 'power to' operator from the above examples.
 ASL (Arithmetic Shift Left). Precedence 6.
Works only on integer numeric values (byte, word, longword). Returns the value of the expression on the LHS with all the bits shifted to the left by the number of places specified by the expression on the RHS. Basically a simple multiply by powers of 2 (per place shifted), and isn't too much different from LSL from a Basic programmers point of view, although technically it is a signed operation. The MSb (Most Significant bit) gets lost from the value and the LSb (Least Significant bit) is cleared to 0.
Code NPrint 1 ASL 1
NPrint 1 ASL 2
NPrint 3 ASL 4
NPrint 15 ASL 3
MouseWait
End
 ASR (Arithmetic Shift Right). Precedence 6.
ASR works only on integer numeric data types, and is the opposite of ASL. It returns the value of the expression on the LHS with all the bits shifted to the right by the number of columns specified in the expression on the RHS. From a BASIC programmer's point of view, it can be used to divide in powers of 2 quickly nd is a signed operation. The MSb retains it's value and the LSb gets lost from the number.
Code NPrint 2 ASR 1
NPrint -4 ASR 1
NPrint -32 ASR 4
NPrint 256 ASR 3
MouseWait
End
 LSL (Logical Shift Left). Precedence 6.
This command only works on integer numeric types (byte, word, longword). It returns the value of the expression on the LHS with all the bits shifted to the left by the number of columns specified in the expression on the RHS. 0's get shifted in to the least significant bits. From a BASIC programmers point of view, it can be seen as a fast multply by powers of 2 (and is similar to ASL).
Code NPrint 1 LSL 1
NPrint 1 LSL 2
NPrint 3 LSL 4
NPrint 15 LSL 3
MouseWait
End
 LSR (Logical Shift Right). Precedence 6.
Only works on integer numeric types (byte, word, longword). Returns the value of the expression on the LHS with all the bits shifted to the right by the number of columns specified in the expression on the RHS. 0's are shifted into the columns at the most significant bit - which makes this an unsigned operation (as opposed to ASR).
Code NPrint 2 LSR 1
NPrint -4 LSR 1
NPrint -32 LSR 4
NPrint 256 LSR 3
MouseWait
End
 & (Bitwise AND). Precedence 5.
Only works on numeric values. Returns the value of the expression on LHS logically anded with the value of the expression on the RHS. For each bit in the expressions, the value of the bit in the result is given by the following table:
A   B   Z (= bit in result)
0   0   0
0   1   0
1   0   0
1   1   1
The AND operator is most useful for checking specific bits in a result, as it can be used to 'mask out' specific bits. For example, everywhere you have a bit with a value of 0 in one of the expressions, the resulting bit will be a 0, whereas if it is a 1, the resulting bit will have the value of the bit in the other expression.
Code foo.w = 15 NPrint foo AND 1
NPrint foo AND 4
NPrint foo AND 7
MouseWait
End
 | (Bitwise OR). Precedence 5.
Only works on numeric types. Returns the value of the expression on the LHS logically or'd with the value of the expression in the RHS. For each bit in the result, it's value is calculated from this table (A and B are the two expressions, Z is the resulting bit).
A   B   Z
0   0   0
0   1   1
1   0   1
1   1   1
The OR operator is most commonly used to set a specific bit to 1. You can see that anywhere there is a 1 in either of the two expressions, the result is a 1. This allows you to force a bit to a 1 in the result.
Code foo.w = 15 NPrint foo OR 1
NPrint foo OR 7
MouseWait
End
 * (Multiplication). Precedence 4.
Arithmetic multiplication, only works on numeric types. Simply returns the value of the expression on the LHS multipled by the expression on the RHS.
Code foo.w = 5*3 NPrint foo
NPrint 12.3 * 2.1
MouseWait
End
 / (Division). Precedence 4.
Arithmetic division, only works on numeric types. Returns the value of the expression on the LHS divided by the expression on the RHS. If the result is being stored in an integer variable, then the value is rounded down to the nearest integer.
Code foo.w = 5/3 NPrint foo
NPrint 69 / 4
MouseWait
End
 + (Addition). Precedence 3.
Works on both numeric and string types, although both the LHS, RHS and the variable you are storing the result in must be of the same type (i.e. you cannot mix addition of numeric and string types). In the case of numeric types, the result is the value of the expression on the RHS added to the value of the expression on the LHS. There is also a fast short method you can use where you do not use the result, so the value of the expression on the RHS gets added directly to the variable on the LHS. In the case of string types, the result is the string on the RHS attached to the end of the string on the LHS.
Code bar = 6 + 5
foo.w = 23
NPrint foo
foo + 2 ; Same as doing foo=foo+2 but quicker (notice result is not used)
NPrint foo
NPrint bar
blah$="Hell "
NPrint blah$+"world"
MouseWait
End
 - (Subtraction). Precedence 3.
Subtraction only works on numeric type variables (i.e. not strings). The result of a calculation containing the subtraction operator is the value of the expression on the RHS subtracted from the value of the expression on the LHS. Like the addition operator, the subtraction operator can be used in a fast method (where the result of the subtraction is not used).
Code bar = 12 - 5
foo.w = 23
NPrint foo
foo - 8 ; Same as doing foo=foo-8 but quicker (notice result is not used)
NPrint foo
NPrint bar
MouseWait
End
 = (Equality). Precedence 2.
The equality operator can be used in two ways, for all variable types (numeric and string). The first allows you to set the value of a variable (on the LHS of the operator) to the value of the expression on the RHS of the operator, and is performed by not using the result of the operation. The second method (testing equality of expressions) is performed by using the result of the operation, which will either be TRUE (if the values on both sides of the operator are the same) or FALSE (if they are different).
Code ; Set value of a variable using the equality operator in the first way
total.w = 23 : NPrint total
head$ = "head"
head$ = head$ + "foo" : NPrint head$
; Test for the equality of two expressions using the second method
NPrint 1 = 1
NPrint total = 24
; Ooh, combination of both methods ;)
foo.w = total = 23
NPrint foo
MouseWait
End
 > (Greater than). Precedence 2.
This operator is only used to test the values of the expressions on either side, in a similar way to the second method of using the equality operator (above). The result of the operation is TRUE if the value of the expression on the LHS is greater than the value of the expression on the RHS, and FALSE otherwise.
Code total = 23
NPrint 1 > 1
NPrint total > 24
NPrint total > 2
MouseWait
End
 < (Less than). Precedence 2.
Similar to the above, this operator is only used to test the values of the expressions on either side of it. The result of the operation is TRUE if the value of the expression on the LHS is less than the value of the expression on the RHS, and FALSE otherwise.
Code total = 23
NPrint 1 < 1
NPrint total < 24
NPrint total < 2
MouseWait
End
 >= (Greater than or equal to). Precedence 2.
Similar to the above, this operator is only used to test the values of the expressions on either side of it. The result of the operation is TRUE if the value of the expression on the LHS is greater than or the same as the value of the expression on the RHS, and FALSE otherwise.
Code total = 23
NPrint 1 >= 1
NPrint total >= 24
NPrint total >= 2
MouseWait
End
 <= (Less than or equal to). Precedence 2.
Similar to the above, this operator is only used to test the values of the expressions on either side of it. The result of the operation is TRUE if the value of the expression on the LHS is less than or the same as the value of the expression on the RHS, and FALSE otherwise.
Code total = 23
NPrint 1 <= 1
NPrint total <= 24
NPrint total <= 2
MouseWait
End
 <> (Not equal to). Precedence 2.
Similar to the above, this operator is only used to test the values of the expressions on either side of it. The result of the operation is TRUE if the value of the expression on the LHS is not the same as the value of the expression on the RHS, and FALSE is they are the same.
Code total = 23
NPrint 1 <> 1
NPrint total <> 24
NPrint total <> 2
MouseWait
End
 AND (Logical AND). Precedence 1 (lowest).
This operator allows you to combine the logical results (TRUE or FALSE) of other operators. If the results of one or both of the expressions on each side of the operator are FALSE, then the result of the AND operation is FALSE, otherwise it is TRUE.
Code NPrint 1=1 AND 2=2
NPrint 1=1 AND 2=3
NPrint 1=2 AND 2=2
NPrint 1=2 AND 2=3
MouseWait
End
 OR (Logical OR). Precedence 1 (lowest).
This operator allows you to combine the logical results (TRUE or FALSE) of other operators. If the results of one or both of the expressions on each side of the operator are TRUE, then the result of the OR operation is TRUE, otherwise it is FALSE.
Code NPrint 1=1 OR 2=2
NPrint 1=1 OR 2=3
NPrint 1=2 OR 2=2
NPrint 1=2 OR 2=3
MouseWait
End


Previous: Defining variables with the built-in types Programming contents Next: More expressions


News | Introduction | Search | List/Forum/IRC Webring | Contact
Installation | Troubleshooting | Programming | Archives | Links
Return to home page
Page last modified: 6th January 2002