Home › Practice › RTL Design › How many lights are on?
How many lights are on?
EasyRTLcombinationalarithmetic
A parking sensor row has 4 bits, one per space, and a 1 means a car is parked there. The display must show how many cars there are.
Concept first. Counting ones is addition in disguise: each bit is a number that is either 0 or 1, so the number of ones is what you get when you add the four bits. How wide must the answer be? Four bits can add up to at most 4, and 4 needs three bits (100). So the answer has one more bit than you might expect. Check with a few inputs: 0110 has two ones, 1111 has four.
What to build.
popcount4(x):xis 4 bits.- Output
n[2:0]is the number of bits ofxthat are 1 (0 to 4). - No clock.
No install and no sign-up needed. You write your answer in the editor and Nishkam checks it.