Count Ones in a 16-bit Word
Problem statement
Return the number of set bits in the unsigned 16-bit value word.
Hardware follow-up: explain how to write the same population count in Verilog and what combinational structure synthesis can infer. The executable task checks the numeric count; it does not compile HDL or grade a circuit drawing.
Function
countOnes16(word: int) → intExamples
Example 1
word = 0return = 0Zero has no set bits.
Example 2
word = 13return = 313 is binary 1101, which has three 1 bits.
Example 3
word = 65535return = 1665535 has all sixteen bits set.
Constraints
0 <= word <= 65535.