Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Constrain exactly K set bits

Hardware interview practice

Constrain exactly K set bits

EasyConstraintsSystemVerilog

Use $countones to randomize a W-bit mask containing exactly K set bits.

Try it in the question bankReason first. Then compare.

Keep this exact question selected while you check your answer and review the full solution.

Practice this question →
Reviewed example

Work through one case

Input
W=8, K=3
Expected output
One valid randomized result: mask=8'b0010_1010

The result contains exactly three one-bits; any other 8-bit value with popcount three is equally valid unless additional weights apply.

What to cover

Requirements

  1. Keep W and K compile-time configurable.
  2. Use a two-state randomized mask.
  3. Treat K outside 0 through W as an unsatisfiable configuration.
asic.fyi · Learn silicon end to end.info@asic.fyi