Relational Operators – Verilog Example

Relational operators in Verilog work the same way they work in other programming languages. The list of relational operators is as follows:

  <      Less Than
  <=     Less Than or Equal To
  >      Greater Than
  >=     Greater Than or Equal To

These are used to test two numbers for their relationship. If operands are of unequal length, Verilog will zero-fill the shorter of the two to make them the same length. Make sure you are careful when comparing two different length inputs! The === and !== allow for checking of X and Z values. This can be used in test benches only, as it is not synthesizable.

relational_operators.v

module relational_operators();

initial 
  begin
    $display("Is 2 <= 3?      %b", 2 <= 3); $display("Is 4 > 6?       %b", 4 > 6);
    $display("Is 1010 >= 10?  %b", 4'b1010 >= 10);
    $display("4'hx >= 10?     %b", 4'hX >= 10);
    #5;
  end
  
endmodule

 

Console output from running above code:

# Is 2 <= 3? 1 # Is 4 > 6?       0
# Is 1010 >= 10?  1
# 4'hx >= 10?     x

Learn Verilog

Modules

Learn VHDL