Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Count set bits with Kernighan's method

Hardware interview practice

Count set bits with Kernighan's method

EasyBit ManipulationSystemVerilog

Return the number of 1 bits in a 32-bit unsigned integer.

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
value = 8'b1011_0100
Expected output
4

Four iterations clear the lowest remaining one-bit before the value reaches zero.

What to cover

Requirements

  1. Clear one set bit per iteration.
  2. Return zero for an all-zero input.
Continue practicing

Related questions

Bit ManipulationCompute Hamming distance→
asic.fyi · Learn silicon end to end.info@asic.fyi