Operator Priority
The table below summarizes the logical, arithmetic and relational operators, presented in groups according to their priority. Priority 1 is the highest priority (performed first), and priority 4 is the lowest (performed last). With two operators of the same priority, the operations are performed left to right.
Priority |
Operator |
Description |
1 |
- |
negate |
1 |
! |
NOT |
2 |
& |
AND |
2 |
<< |
shift left |
2 |
>> |
shift right |
2 |
* |
multiply |
2 |
/ |
unsigned division |
2 |
% |
modulus |
3 |
+ |
add |
3 |
- |
subtract |
3 |
# |
OR |
3 |
$ |
XOR: exclusive OR |
3 |
!$ |
XNOR: exclusive NOR |
4 |
== |
equal |
4 |
!= |
not equal |
4 |
< |
less than |
4 |
<= |
less than or equal |
4 |
> |
greater than |
4 |
>= |
greater than or equal |
When you have operators of the same priority in the same expression, the operations are performed from left to right. You can use parentheses to change the order of evaluation, with the operation in the innermost set of parentheses performed first. Some examples of expressions are given below. Note how the order of operations and the use of parentheses affect the evaluated result.
Expression |
Result |
Description |
2 * 3/2 |
3 |
operators with same priority |
2 * 3 / 2 |
3 |
spaces are insignificant |
2 * (3/2) |
2 |
fraction is truncated |
2 + 3 * 4 |
14 |
multiply first |
(2 + 3) * 4 |
20 |
add first |
2#4$2 |
4 |
|
2#(4$2) |
6 |
|
2 == ^HA |
0 |
false |
14 == ^HE |
-1 |
true |
See Also