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

Operators: Verilog Shift Operators

Verilog Examples

Runnable example from the Verilog Operators tutorial (Verilog Shift Operators)

Open in IDE
testbench.v
module des;
 reg [7:0] data;
 integer i;

 initial begin
 // Left shift: multiply by powers of 2
 data = 8'h1; // Start with 0000_0001
 $display ("Original data = 'd%0d or 'b%0b", data, data);
 for (i = 0; i < 8; i +=1 ) begin
 $display ("data << %0d = 'b%b", i, data << i); // Shift left by i positions
 end

 // Right shift: divide by powers of 2
 data = 8'h80; // Start with 1000_0000 (128 decimal)
 $display ("Original data = 'd%0d or 'b%0b", data, data);
 for (i = 0; i < 8; i +=1 ) begin
 $display ("data >> %0d = 'b%b", i, data >> i); // Shift right by i positions
 end

 // Demonstrate zero-filling on right shift
 data = 8'h1;
 $display ("data >> 1 = 'b%b", data >> 1); // 0000_0001 >> 1 = 0000_0000
 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.