TapHome

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:

GroupOperatorsExample
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
LogicalAND, XOR, ORTe > 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.

  1. * /
  2. + -
  3. << >>
  4. < <= > >=
  5. = <> !=
  6. &
  7. | ^
  8. AND
  9. XOR
  10. OR

Unlike C or JavaScript, the bitwise operators bind tighter than AND and OR but looser than the comparisons. a & 1 > 0 means a & (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: TRUE

A 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 error

The 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:

TapHomeExcel, Google SheetsExample
a & bBITAND(a, b)3 & 1 is 1
a | bBITOR(a, b)2 | 1 is 3
a ^ bBITXOR(a, b)6 ^ 3 is 5
a << nBITLSHIFT(a, n)8 << 2 is 32
a >> nBITRSHIFT(a, n)32 >> 2 is 8

In Excel ^ is a power; in TapHome it is the bitwise XOR. Use POWER for powers. IF, AND, OR and MOD have the same meaning in both, but in TapHome AND and OR are 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.