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

Interview Questions Set 9: Write Verilog code to store fractional number

Verilog Examples

Runnable example from the Verilog Interview Questions Set 9 tutorial (Write Verilog code to store fractional number)

Open in IDE
testbench.v
module fixed_point_example();

  reg [6:0] integer_part;         // 7 bits for integer part
  reg [3:0] fractional_part;      // 4 bits for fractional part

  reg [10:0] fixed_point_number;  // 11 bits for Q7.4 number

  initial begin
    integer_part = 5;
    fractional_part = 8;          // This is 0.5 in Q7.4 format

    fixed_point_number = {integer_part, fractional_part}; // Concatenate integer and fractional parts
    $display("Fixed-point number = %f", fixed_point_number / 16.0); // Prints 5.500000
  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.