Prev Problem
Next Problem

54. Population Count

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

One thing I want to add you can't use count += in[i] because here tool does not support the dependency. So you need to use count = count + in[i] 

Code

module popcount8 (
    input  [7:0] in,
    output reg [3:0] count
);
    integer i; // loop variable must be declared (synthesizable)
    always @* begin
        count = 4'd0;          // default to avoid latch

        // Use a for-loop to accumulate the number of '1' bits
        for (i = 0; i < 8; i = i + 1) begin
        // Write code here
            count = count + in[i];

        end
    end
endmodule

 

Was this helpful?
Upvote
Downvote