Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Number of 1s in a binary string

Status
Not open for further replies.

meher81

Member level 1
Joined
Mar 4, 2010
Messages
35
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,286
Location
Iran
Activity points
1,479
I have a n-bit binary string. There are not more than two 1s in it. How can I found the simplest way to detect the number of 1's in it?
Thanks
 

Rotate the string one bit at a time; check n-th bit (or zeroth bit) if it is 1, increase the count. You have to rotate string n time to appear all bits in n-th position (on in zeroth position).

Thanks,
 

Thank you
But I want to find the solution without using any loop. Is there any logical statement for it?
 

You can also use a char pointer to check each char of the array which should be faster that rotate but without a loop I don't see a way to do what you want.

Alex
 

A general way to do this is the combinatorial loop below
Size m to handle the max value possible (generally log2(n)+1)

integer count_i;
reg [m:0] count;
always @(*) begin
count = 0;
for (count_i=0; count_i<n; count_i = count_i + 'd1 )
count = count + vector[count_i]; // count 1s in vector
end

How efficient it is going to depend a lot on your synthesizer.

I would be interested in better ways to do this.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top