binary no

  • Follow


How can we count no of ones in a binary no.
plz don't tell by linear search. if any effecient logic u have in
mind...plz tell me.

Thanks in advance.

0
Reply nehilparashar (24) 7/19/2007 11:54:33 AM

On Thursday 19 Jul 2007 5:24 pm, Nehil <nehilparashar@gmail.com>
wrote in message
<1184846073.977293.136820@n60g2000hse.googlegroups.com>:

> How can we count no of ones in a binary no.
> plz don't tell by linear search. if any effecient logic u have in
> mind...plz tell me.
> 
> Thanks in advance.

Since your question is a general algorithmic one,
news:comp.programming will be a better forum than this one.


0
Reply santosh.k83 (3969) 7/19/2007 12:10:51 PM


Nehil wrote:

> How can we count no of ones in a binary no.

Binary "no" is 0, which has zero 1's in it.

> plz don't tell by linear search. if any effecient logic u have in
> mind...plz tell me.

Your keyboard is broken.

One trick is to use `whatever = whatever & (whatever - 1)`. (Of /course/
in a loop.)

Of course, googling "count bits" couldn't possibly get any answers, because
if it did, you wouldn't have asked the question.

-- 
Chris "cranky today" Dollin

Hewlett-Packard Limited                                          registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN          690597 England

0
Reply chris.dollin (1683) 7/19/2007 1:20:03 PM

Nehil wrote:

> How can we count no of ones in a binary no.
> plz don't tell by linear search. if any effecient logic u have in
> mind...plz tell me.

Please spell out your words in this newsgroup.

Fast ways are lookup table and hardware adder logic.  In terms of the 
lookup table, the number can be split into n-bit chunks for lookup into 
a table with 2^n entries.

-- 
Thad
0
Reply ThadSmith (1279) 7/19/2007 4:10:20 PM

3 Replies
22 Views

(page loaded in 0.256 seconds)


Reply: