Precedence and Width
Verilog evaluates expressions according to strict operator precedence and expression bit-width rules. Ambiguous expressions compile cleanly without warnings, but they synthesize unintended circuit logic. This article explains the operator precedence hierarchy and bit-width expansion rules.
Operator Precedence Hierarchy
Higher table rows bind with higher precedence. Operators within the same row associate from left to right:
| Priority Level | Operators | Description |
|---|---|---|
| 1 | +, -, !, ~, &, ~&, |, ~|, ^, ~^ |
Unary arithmetic, logical negation, bitwise NOT, and reductions |
| 2 | ** |
Exponentiation |
| 3 | *, /, % |
Multiplication, division, and modulo |
| 4 | +, - |
Binary addition and subtraction |
| 5 | <<, >>, <<<, >>> |
Bitwise shift operations |
| 6 | <, <=, >, >= |
Relational magnitude comparisons |
| 7 | ==, !=, ===, !== |
Equality comparisons |
| 8 | & |
Bitwise AND |
| 9 | ^, ~^ |
Bitwise XOR and XNOR |
| 10 | | |
Bitwise OR |
| 11 | && |
Logical AND |
| 12 | || |
Logical OR |
| 13 | ?: |
Ternary conditional operator |
Common Operator Precedence Traps
Engineers encounter four frequent syntax errors when writing multi-operator expressions:
-
Equality Binds Tighter Than Bitwise AND:
wire [7:0] a, b; // Error: Evaluates as a & (b == 8'hFF); 1-bit boolean comparison is ANDed with a wire [7:0] wrong = a & b == 8'hFF; // Correct: Parentheses force bitwise masking before equality comparison wire right = (a & b) == 8'hFF; -
Shift Operators Bind Tighter Than Addition: The expression
a + b << 2parses asa + (b << 2). Use explicit parentheses to shift the sum:(a + b) << 2. -
Bitwise Precedence Hierarchy: Bitwise AND (
&) binds tighter than XOR (^), and XOR binds tighter than OR (|). Always use explicit parentheses in complex masking equations:(a & b) | (c & d). -
Ternary Operator Low Precedence: The conditional operator
?:possesses the lowest precedence level. When nesting conditional operators inside concatenations or arithmetic expressions, enclose each ternary expression in parentheses:{sel ? a : b, c}becomes{(sel ? a : b), c}.
The Rule of Explicit Parentheses
Do not rely on implicit precedence rules. Insert parentheses whenever an expression combines multiple operator types:
wire result = ((a & mask) == expected) && (|status_bits) && (!fault_flag);
Explicit parentheses eliminate ambiguity, clarify design intent, and generate identical gate netlists.
Expression Bit-Width Determination
Expression evaluation width falls into two categories: context-determined and self-determined.
Context-Determined Expressions
In a standard continuous assignment, Verilog determines the calculation bit width from the widest operand or the destination variable:
wire [7:0] a, b;
wire [8:0] sum = a + b; // Evaluated at 9 bits: carry preserved in bit 8
wire [15:0] product = a * b; // Evaluated at 16 bits: full product preserved
When the destination is narrower than intermediate calculations, high bits are truncated:
wire [3:0] p = 4'hF;
wire [7:0] q = 8'h01;
wire [3:0] r = p + q; // Evaluated at 8 bits (8'h10), then truncated to 4 bits: 4'h0
Self-Determined Expressions
In a self-determined expression, the operation bit width depends entirely on its own operands. Surrounding contexts do not expand the calculation width:
- Relational and equality comparisons (
>,<,==). - Expressions enclosed inside concatenation braces
{}. - Shift amounts in shift operations (
<<,>>). - Condition expressions in ternary operators (
?:). - Arguments passed to user-defined functions and system tasks.
Self-determined rules frequently cause overflow detection bugs:
wire [7:0] a, b;
// Defect: Addition evaluates at 8 bits; 8-bit sum wraps to 0 and never exceeds 255
wire ovf_never = (a + b) > 8'hFF;
// Correct: Zero-extension forces 9-bit self-determined addition
wire ovf_right = ({1'b0, a} + {1'b0, b}) > 9'd255;
Similarly, intermediate vector declarations truncate bit widths:
wire [7:0] a, b;
wire [7:0] t = a * b; // Multiplier computes 8-bit result; upper 8 bits lost
wire [15:0] p = t; // Zero-extension cannot recover discarded bits
Prefix operands with explicit zero-extension bits {1'b0, operand} to retain carry and overflow information.
Static Code Linting
Automated linters detect precedence and width mismatches during continuous integration. Open-source tools like Verilator (invoked with -Wall) and commercial EDA linters flag:
- Truncation on vector assignments.
- Width mismatches in comparisons.
- Mixed signed and unsigned arithmetic.
- Complex expressions without explicit parentheses.
Configure design workflows to require clean lint runs before running logic synthesis.
Practice Exercise
Identify the syntax issue in each expression, describe the actual calculation performed, and provide the corrected code:
wire [7:0] a = 8'h0F;
wire [7:0] b = 8'hF0;
wire check1 = a | b == 8'hFF; // Intent: Test if (a OR b) equals 8'hFF
wire [7:0] check2 = a ^ b << 2; // Intent: Shift (a XOR b) left by 2 bits
wire check3 = a + b > 8'hFF; // Intent: Detect 8-bit addition overflow
Solution
-
check1: Parses asa | (b == 8'hFF). The equality evaluates to 0, resulting in8'h0F | 0 = 8'h0F. Truncated to a 1-bit wire, it evaluates to 1 for the wrong reason.- Correction:
wire check1 = (a | b) == 8'hFF;
- Correction:
-
check2: Parses asa ^ (b << 2). The shift executes first (8'hF0 << 2 = 8'hC0), producing8'h0F ^ 8'hC0 = 8'hCF.- Correction:
wire [7:0] check2 = (a ^ b) << 2;
- Correction:
-
check3: Relational comparison operands are self-determined. The addition executes at 8 bits and wraps on overflow. The condition never evaluates to true.- Correction:
wire check3 = ({1'b0, a} + {1'b0, b}) > 9'd255;
- Correction:
This article completes the study of Verilog operators. Section 4.4 examines structural modeling, module instantiation hierarchy, and parameter overrides.