|
|||||
Precedence of numeric operators | |||||
Prerequisite Concepts |
Operator precedence is the order in which operators are evaluated. When two operators apply to the same term, the one with the higher precedence is evaluated first.
Monadic operators are operators which only take a single operand. Dyadic take two operands. Monadic operators always have precedence over dyadic operators.
The precedence table for numeric operators from highest precedence to lowest is:
complement
modulo
, shift
, and mask
have the same precedence
difference
, and union
have the same precedence
Bit-oriented and arithmetic operators may be combined in numeric expressions. Parentheses can be used to group subexpressions to override the precedence, and leading minus signs can be parenthesized with their arguments as well.
This example illustrates the order of evaluation of numeric operators.
11 mask 5 * 3 + 14 union 35 shift 5 / 56 + complement 3is equivalent to:
((((11 mask 5) * 3) + 14) union ((35 shift 5) / 56)) + (complement 3)
This is evaluated as follows, starting with the innermost parentheses:
((((11 mask 5) * 3) + 14) union ((35 shift 5) / 56)) + (complement 3)
(((1 * 3) + 14) union ((35 shift 5) / 56)) + (complement 3)
((3 + 14) union ((35 shift 5) / 56)) + (complement 3)
(17 union ((35 shift 5) / 56)) + (complement 3)
(17 union (1120 / 56)) + (complement 3)
(17 union 20) + (complement 3)
21 + (complement 3)
21 + -4
-17
This example illustrates how parentheses can be used to override the precedence of numeric operators. The first example evaluates as 17, while the second evaluates to 21.
3 * 5 + 2 3 * (5 + 2)
Prerequisite Concepts Arithmetic and comparison operators Bit-oriented arithmetic Numeric expressions |
---- |