Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Popcount an arbitrary bit vector

Hardware interview practice

Popcount an arbitrary bit vector

EasyVerification UtilitiesSystemVerilog

Return the number of 1 bits in a packed bit vector.

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
bits = 12'b1010_0011_0101
Expected output
6

The count covers the full supplied vector width rather than assuming a fixed 32-bit value.

What to cover

Requirements

  1. Derive the loop bound from $bits(v).
  2. Count only known 1 values.
asic.fyi · Learn silicon end to end.info@asic.fyi