Hardware interview practice
Constrain exactly K set bits
Use $countones to randomize a W-bit mask containing exactly K set bits.
Reviewed example
Work through one case
Input
W=8, K=3Expected output
One valid randomized result: mask=8'b0010_1010The 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
- Keep W and K compile-time configurable.
- Use a two-state randomized mask.
- Treat K outside 0 through W as an unsatisfiable configuration.
