Prev Problem
Next Problem

54. Population Count

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

 

Code

//==================================================
// Module: popcount8
// Description: Counts how many bits in an 8-bit input are '1'.
//==================================================
module popcount8 (
    input  wire [7:0] in,
    output reg  [3:0] count   // Range: 0–8
);

    integer i;

    always @(*) begin
        count = 4'd0;  // Initialize to avoid latches
        for (i = 0; i < 8; i = i + 1) begin
            count = count + in[i];
        end
    end

endmodule

 

Was this helpful?
Upvote
Downvote