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

Verilog Operators

Simple arithmetic operators for addition, subtraction, etc

Open in IDE
design.v
module des;
    reg [7:0] data1;
    reg [7:0] data2;

    initial begin
        data1 = 45; // 8-bit value
        data2 = 9; // 8-bit value

        $display ("Add + = %d", data1 + data2); // 45 + 9 = 54
        $display ("Sub - = %d", data1 - data2); // 45 - 9 = 36
        $display ("Mul * = %d", data1 * data2); // 45 * 9 = 405, truncated to 8 bits = 149
        $display ("Div / = %d", data1 / data2); // 45 / 9 = 5 (integer division)
        $display ("Mod %% = %d", data1 % data2); // 45 % 9 = 0 (no remainder)
        $display ("Pow ** = %d", data2 ** 2); // 9^2 = 81 (simulation only)
    end
endmodule
testbench.v
// Enter testbench code
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.