Continue to Site

# counters question to count no of ones in a given data stream

Status
Not open for further replies.

#### Ekta Rao

##### Newbie level 1
How to count no of ones in a data stream of 4 bits

What is up with all the vague questions lately that require clarification?

Do you mean the data stream is made up of nibbles? If so, an adder (accumulator) and a lookup table (i.e. simple ROM conversion to a number of 1's count)

Code Verilog - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
always @(posedge clk) begin
case (nibble)
4'b0000 : num_1s <= 3'd0;
4'b0001 : num_1s <= 3'd1;
4'b0010 : num_1s <= 3'd1;
4'b0011 : num_1s <= 3'd2;
4'b0100 : num_1s <= 3'd1;
4'b0101 : num_1s <= 3'd2;
4'b0110 : num_1s <= 3'd2;
4'b0111 : num_1s <= 3'd3;
4'b1000 : num_1s <= 3'd1;
4'b1001 : num_1s <= 3'd2;
4'b1010 : num_1s <= 3'd1;
4'b1011 : num_1s <= 3'd3;
4'b1100 : num_1s <= 3'd2;
4'b1101 : num_1s <= 3'd3;
4'b1110 : num_1s <= 3'd3;
4'b1111 : num_1s <= 3'd4;
endcase
accum <= init_accum ? 0 : (accum + num_1s);
end

How to count no of ones in a data stream of 4 bits

Using a ripple counter ?

Hi,

A counter with reset and enable.
Reset with the start of a new 4 bit frame
Clock it with every bit
Enable when incoming bit is high.

Klaus

What is up with all the vague questions lately that require clarification?

I think a sticky note is needed which can contain explanations regarding
1> How to ask question related to h/w design & its implementation, what things need to be included in the Qs for others to better and quickly understand the scenario.
2> Many a times members ask Qs such as what is the command for this and that without searching the corresponding user or command ref manuals (mostly newbies don't even know such things exist). So a description can be included regarding the likely place to be searched for such documents.
3> some other newbie Q scenario, which often gets repeated (doesn't come to my mind right now).

Last but not the least come up with a method to enforce new members to read such a sticky thread before posting the first time.

I am only talking about the forums related to digital hardware design. I have no idea about the situation in other sub-forums.

Status
Not open for further replies.