top of page

10.1 Operators Precedence

Understanding Operator Precedence

 

In Verilog, just like in mathematics, the order in which operations are evaluated matters significantly. Understanding operator precedence is crucial for writing correct RTL code and avoiding subtle bugs that can be difficult to debug. A misunderstanding of precedence can lead to code that simulates correctly but synthesizes incorrectly, or worse, produces completely unexpected hardware behavior.

​

At VLSI Mentor, we emphasize that mastering operator precedence is not just about memorizing a table—it's about understanding how your code translates to hardware and writing clear, maintainable RTL.

​

Operator precedence determines the order in which operations are evaluated in an expression. Just like in mathematics where multiplication comes before addition, Verilog has a specific hierarchy of operators. Understanding precedence is critical for writing correct RTL code and avoiding subtle bugs.

​

10.1.1. Verilog Operator Precedence Table

​

The table below lists Verilog operators from highest to lowest precedence:

op1.png

10.1.2. Practical Examples of Precedence

Example 1: Mixed Arithmetic and Logical Operations

Verilog

Output:

a + b * c = 20

(a + b) * c = 30

a + b * c / 2 = 15

Example 2: Bitwise vs Logical Operations

Verilog

Example 3: Shift and Arithmetic

Verilog

Example 4: Relational and Bitwise

Verilog

Example 5: Complex Expression Breakdown

Verilog

10.1.3. Best Practices for Operator Precedence

Best Practices for Managing Precedence

10.1.3.1. Always Use Parentheses for Clarity

Verilog

10.1.3.2. Use Intermediate Variables

Verilog

10.1.3.3. Document Non-Obvious Precedence

Verilog

10.1.3.4. Group Related Operations

Verilog

10.1.3.5. Format for Readability

Verilog

10.1.3.5. Format for Readability

Verilog

10.1.4. Common Precedence Pitfalls

Pitfall 1: Shift Operations

Verilog

Pitfall 2: Bitwise vs Logical

Verilog

Pitfall 3: Equality and Bitwise Operations

Verilog

Pitfall 4: Comparison in Complex Expressions

Verilog

Pitfall 5: Conditional with Low Precedence

Verilog

10.1.5. Precedence in Different Contexts

10.1.5.1. In Conditional Statements

Verilog

10.1.5.2. In Arithmetic Expressions

Verilog

10.1.5.3. In Bit Manipulation

Verilog

10.1.6. Practical Design Example

Example: Status Register Decoder

Verilog

Example: ALU Operation with Clear Precedence

Verilog

10.1.7. Operator Precedence Quick Reference

Memory Aid: "Please Use My Dear Aunt Sally's Relational Equation, But Learn Carefully"

**P**arentheses  
 

**U**nary  
 

**M**ultiply/**D**ivide  
 

**A**dd/**S**ubtract  
 

**S**hifts  
 

**R**elational  
 

**E**quality  
 

**B**itwise  
 

**L**ogical  
 

**C**onditional 

​

Key Rules to Remember

​

1. Arithmetic before Shift: `a + b << 2` = `a + (b << 2)`

​

2. Shift before Relational: `a << 2 > b` = `(a << 2) > b`

​

3. Relational before Equality: `a < b == c < d` = `(a < b) == (c < d)`

​

4. Bitwise before Logical: `a & b || c` = `(a & b) || c`

​

5. Logical AND before OR: `a || b && c` = `a || (b && c)`

​

When in Doubt: USE PARENTHESES!

10.1.8. Synthesis Considerations

10.1.8.1. Precedence Affects Hardware

Verilog

10.1.8.2. Critical Path Implications

Verilog

10.1.9. Debugging Tips

Use $display for Complex Expressions

Verilog

Break Down Complex Expressions

Verilog

Verilog operators

and operands

Arithmetic operator

© Copyright 2025 VLSI Mentor. All Rights Reserved.©

Connect with us

  • Instagram
  • Facebook
  • Twitter
  • LinkedIn
  • YouTube
bottom of page