Introduction
Operator precedence tells the order in which parts of a Verilog expression are calculated. It helps avoid confusion and errors in your code.
result = operand1 operator operand2; // Operators follow precedence rules
result = a + b * c;
result = (a + b) * c;
flag = a && b || c;
flag = (a && b) || c;
module precedence_example; reg [3:0] a = 4, b = 2, c = 3; reg [7:0] result1, result2; initial begin result1 = a + b * c; // Without parentheses result2 = (a + b) * c; // With parentheses $display("result1 = %d", result1); $display("result2 = %d", result2); end endmodule
reg [3:0] a = 4; reg [3:0] b = 2; reg [3:0] c = 1; wire [3:0] result = a + b * c;
result?wire result = a & b | c;
a, b, and c are 1-bit wires.wire [3:0] x = 4; wire [3:0] y = 2; wire [3:0] z = 1;
wire [3:0] out = x + y << z & 3;
out?Pass the test and this pattern moves unknown