Population Count (Hamming Weight)
beginner · combinational · 50 XP
Sign in to write your solution, run it in the simulator and submit it for grading.
Sign in or create a free account →Problem
Population Count (Hamming Weight)
Input: 8-bit `in[7:0]`. Output: 4-bit `count[3:0]` — the number of 1-bits in `in`.
Implement by summing all individual bits:
```
count = in[0] + in[1] + in[2] + in[3] + in[4] + in[5] + in[6] + in[7];
```
**Examples**
- `in=8'h00 → count=0`
- `in=8'hFF → count=8`
- `in=8'hAA (10101010) → count=4`
- `in=8'h01 → count=1`
**Note:** This operation is used in cryptography, error correction, and branch predictors. CPUs expose it as the `POPCNT` instruction.
Constraints
- Sum all 8 bits explicitly. Output must be 4-bit wide (max value 8 fits in 4 bits).
Examples
Input: in=8'h00
Output: count=4'd0
Input: in=8'hFF
Output: count=4'd8
Common questions
- What does the Population Count (Hamming Weight) problem ask you to build?
- Population Count (Hamming Weight) Input: 8-bit `in[7:0]`. Output: 4-bit `count[3:0]` — the number of 1-bits in `in`.
- What constraints does Population Count (Hamming Weight) have?
- Sum all 8 bits explicitly. Output must be 4-bit wide (max value 8 fits in 4 bits).
- What is an example input and output for Population Count (Hamming Weight)?
- Input in=8'h00 gives output count=4'd0.
- Which Verilog concepts does Population Count (Hamming Weight) practise?
- combinational, interview, vectors.