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 5: Write a constraint to detect odd numbers of ones in a

SystemVerilog Interview Questions

Runnable example from the SystemVerilog Interview Questions Set 5 tutorial (Write a constraint to detect odd numbers of ones in an 8-bit sequence.)

Open in IDE
testbench.sv
class ABC;
  rand bit [7:0] data;

  constraint c_data { $countones(data) % 2 != 0; }
endclass

module tb;
  initial begin
    ABC m_abc = new;

    for (byte i = 0; i < 20; i++) begin
      m_abc.randomize();
      $display("data = 0b%0b", m_abc.data);
    end
  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.