Operators
Arithmetic, comparison, logical and bitwise operators, and the two string operators. Every expression fits on one line and parentheses group as usual.
Arithmetic
| Operator | Meaning | Example |
|---|---|---|
+ - * | add, subtract, multiply | 15 + 42 * 22 |
/ | division; integer division for integers, truncating toward zero | 5 / 3 is 1, 10.0 / 4.0 is 2.5 |
% | remainder (integers) | 5 % 3 is 2 |
-x | unary minus, also on floats | f = -a |
Mixing types widens the result: int with long gives long, anything with float gives float. Comparisons and the bitwise and shift operators do not widen an int variable next to a long (write as long), but an integer literal, or an expression made only of literals such as a macro, becomes a long by itself: strlen(s) == 0, x >> 4 and n > MAX - 1 compile as written. This way an int counter never meets a long length unnoticed.
Comparison
== != < > <= >= produce 1 or 0. A comparison is an ordinary value: it can be stored, returned, passed to a function or used in arithmetic.
def lt(a: int, b: int) -> int:
return a < b
p = (3 < 5) // 1
q = lt(2, 5) + lt(5, 2) // 1 + 0
On two str operands, == and != compare the contents (byte by byte, through rt_strcmp), not the addresses. The other comparison operators are not defined on strings.
Logical
| Operator | Meaning |
|---|---|
and | true when both operands are true (non-zero, or a non-empty str: see Control flow); short-circuits (the right side is not evaluated when the left one is false) |
or | true when either operand is true; short-circuits |
not | 1 when the operand is false, otherwise 0 |
if x < y and y > x and x != y:
print("ordered\n")
if not (a > b):
print("a <= b\n")
Bitwise and shifts
| Operator | Meaning | Example |
|---|---|---|
& | ^ | and, or, xor | ok = ok | 2 |
<< | shift left | 1 << 4 is 16 |
>> | arithmetic shift right (the sign is preserved) | -8 >> 1 is -4 |
The shift count can be a variable. On long operands the shift is a 64-bit shift: (1 as long) << 40.
Precedence
From tightest to loosest binding. Shifts sit below addition but above the bitwise operators and comparisons, which differs from C; when in doubt, parenthesise.
| Level | Operators | Check |
|---|---|---|
| 1 | unary -, not, as, &name | |
| 2 | * / % | |
| 3 | + - | 2 + 3 << 1 is 10 |
| 4 | << >> (left associative) | 1 << 2 << 3 is 32 |
| 5 | & | ^ | 9 << 1 & 6 is 2 |
| 6 | == != < > <= >= | 1 << 4 == 16 is 1 |
| 7 | and | |
| 8 | or |
Strings
| Operator | Meaning | Example |
|---|---|---|
+ | concatenation; a char operand is appended as one character | "Hello " + name + "\n", "" + c |
* | repetition, in either order | "ab" * 3 is "ababab" |
s[i] | the i-th byte as a char (no bounds check) | s[0] == 'h' |
Both + and * allocate a new string in the arena; see Strings for the cost model and the in-place fast path of x = x + t.
Assignment
Compound assignment works on names, array elements and fields: x += e is exactly x = x + (e), and so are -= *= /= %= <<= >>= &= |= ^=; s += "txt" appends to a string. They also work in the init and step of a for (for i = 0, k = 2; i < n; i += k, k += 1:). A target containing a call (v[f()] += 1) is rejected, because the call would run twice. x++ and x-- are statements. Multiple targets take the values of a multi-return function: q, r = divmod(17, 5).
Other operators
| Syntax | Meaning | Page |
|---|---|---|
expr as T | cast | Variables & types |
&name | address of a variable | Pointers |
new T, new T[n] | allocate an object or an array | Classes, Arrays |
obj.field, a[i][j] | field and element access, chainable | |
typeid(Class) | the numeric id of a class, for variadic arguments | Functions |