Operators
Operators of the TapHome script language: arithmetic, comparison, bitwise and logical operators, their 10 precedence levels, the a & 1 > 0 trap, short-circuit evaluation and Excel equivalents.
Operator list
The script language has arithmetic, comparison, bitwise and logical operators:
| Group | Operators | Example |
|---|---|---|
| Unary | - (negation), +, ! (logical NOT) | !IsRaining |
| Multiplicative | *, / | (20.5 + 9.5) / 2 is 15 |
| Additive | +, - | "T=" + Te joins text |
| Shift | <<, >> | 8 << 2 is 32 |
| Relational | <, <=, >, >= | Te > 25 |
| Equality | = (equal), <> or != (not equal) | Mode <> 2 |
| Bitwise AND | & | 3 & 1 is 1 |
| Bitwise OR, XOR | |, ^ | 2 | 1 is 3 |
| Logical | AND, XOR, OR | Te > 25 AND Hu > 60 |
| Assignment | := and the compound forms +=, -=, *=, /=, <<=, >>=, &=, |=, ^= | x += 1 |
XOR on two numbers is a bitwise XOR: 5 XOR 1 is 4. On TRUE and FALSE it is the logical XOR.
The language has no NOT, ==, %, **, && or ||. Use !, =, MOD, POWER, AND and OR. 1 == 1 fails with Wrong number of literals and operators in expression, NOT x with Unknown variable or constant 'NOT'.
Precedence
Operators are evaluated in this order, from the highest precedence to the lowest. Operators of the same level are evaluated from left to right; parentheses ( ) override the order.
*/+-<<>><<=>>==<>!=&|^ANDXOROR
Unlike C or JavaScript, the bitwise operators bind tighter than
ANDandORbut looser than the comparisons.a & 1 > 0meansa & (1 > 0). Always write(a & 1) > 0.
VAR flags := 2;
flags & 2 > 0;
# Result: 0, read as flags & (2 > 0), which is flags & 1
(flags & 2) > 0;
# Result: TRUEA compound assignment has the precedence of its operation: x -= a - b is evaluated as (x -= a) - b and x *= 1 + 2 as (x *= 1) + 2. Put the right side in parentheses:
VAR total := 10;
total -= (a - b);Short-circuit evaluation
AND and OR evaluate the right side only when the left side does not decide the result yet. XOR always evaluates both sides. A condition can therefore protect the expression next to it:
va2 <> 0 AND va1 / va2 > 1;
# Result: FALSE when va2 is 0, without the Division by zero errorThe right side is not evaluated when va2 is 0. The same protection for a value is IF, which evaluates only the selected branch.
Excel equivalents
The bit operators correspond to these Excel and Google Sheets functions:
| TapHome | Excel, Google Sheets | Example |
|---|---|---|
a & b | BITAND(a, b) | 3 & 1 is 1 |
a | b | BITOR(a, b) | 2 | 1 is 3 |
a ^ b | BITXOR(a, b) | 6 ^ 3 is 5 |
a << n | BITLSHIFT(a, n) | 8 << 2 is 32 |
a >> n | BITRSHIFT(a, n) | 32 >> 2 is 8 |
In Excel
^is a power; in TapHome it is the bitwise XOR. UsePOWERfor powers.IF,AND,ORandMODhave the same meaning in both, but in TapHomeANDandORare operators placed between the operands.
See the example of bit operations in Google Sheets: https://docs.google.com/spreadsheets/d/1hF5FMpGMJbgYh-YLwWrq2n186_ATyGyLUb689__IhLY/edit?usp=sharing, or try the interactive tool at http://bitwisecmd.com/. Functions that read and write single bits and bytes are on Bit operations.