Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Arithmetic Operators: Expression Bit Lengths and Sizing

Verilog Examples

Runnable example from the Verilog Arithmetic Operators tutorial (Expression Bit Lengths and Sizing)

Open in IDE
testbench.v
module division_by_zero_example;
 reg [7:0] dividend;
 reg [7:0] divisor;
 reg [7:0] result_division;
 reg [7:0] result_modulus;

 initial begin
 dividend = 8'd10; // Decimal 10
 divisor = 8'd0; // Decimal 0

 // Division by zero
 result_division = dividend / divisor;
 $display("At time %0t: Result of %d / %d = %h", $time, dividend, divisor, result_division);

 // Modulus by zero
 result_modulus = dividend % divisor;
 $display("At time %0t: Result of %d %% %d = %h", $time, dividend, divisor, result_modulus);

 divisor = 8'd5; // Change divisor to a non-zero value
 result_division = dividend / divisor;
 $display("At time %0t: Result of %d / %d = %h", $time, dividend, divisor, result_division);
 end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.