← ProblemsVerilog Language / More Verilog Features

Population count

36%loops-generatevectors

A population count circuit counts how many bits of a vector are 1. Build one for a 255-bit input in[254:0], producing the count on the 8-bit output out (the count ranges from 0 to 255, which just fits in 8 bits).

This is a classic accumulate-in-a-loop pattern: start the count at zero, then add each input bit in turn inside a combinational for loop.