Operators and Expressions
Expression operators and data types
| Operator token | Operator name | Operand data types | Result data type |
|---|---|---|---|
?: |
Conditional operator | Any plain-data type or reference type (condition is Boolean) | Same as operands |
- |
Unary arithmetic negation operator | Numeric | Same as operand |
~ |
Unary bitwise negation operator | Integer | Same as operand |
! |
Unary Boolean negation operator | Boolean | Boolean |
& | ^ |
Unary bitwise reduction operators | Integer | Bit |
+ - * / ** |
Binary arithmetic operators | Numeric | Numeric |
% |
Binary modulus operator | Integer | Integer |
& | ^ |
Binary bitwise operators | Integer | Integer |
>> << |
Binary shift operators | Integer | Integer |
&& || |
Binary Boolean logical operators | Boolean | Boolean |
< <= > >= |
Binary relational operators | Numeric | Boolean |
== != |
Binary logical equality operators | Any plain-data type or reference type | Boolean |
cast |
Data type conversion operator | Numeric, Boolean, enum | Casting type |
in |
Binary set membership operator | Any plain-data type | Boolean |
[expression] |
Index operator | Array, list, map | Same as element of collection |
[expression] |
Bit-select operators | Integer | Integer |
[expression:expression] |
Part-select operator | Integer | Integer |
Result width
The data types above say what kind of value an operator returns. Its bit size and
signedness follow the evaluation rules for numeric expressions (PSS 3.1, 8.7): in
bit [16] z = x + y; the 16-bit target is propagated into x + y, so the sum does not
overflow at the operands' width.
Operator precedence and associativity
| Operator | Associativity | Precedence |
|---|---|---|
() [] |
Left | 1 (Highest) |
cast |
Right | 2 |
- ! ~ & | ^ (unary) |
2 | |
** |
Left | 3 |
* / % |
Left | 4 |
+ - (binary) |
Left | 5 |
<< >> |
Left | 6 |
< <= > >= in |
Left | 7 |
== != |
Left | 8 |
& (binary) |
Left | 9 |
^ (binary) |
Left | 10 |
| (binary) |
Left | 11 |
&& |
Left | 12 |
|| |
Left | 13 |
?: (conditional operator) |
Right | 14 (Lowest) |