Re: [algogeeks] You have given a number N. You need to find out total number of set bits from 0 to N.

2011-09-11 Thread Amol Sharma
in GCC a built in function can do it for u __builtin_popcount() http://ideone.com/RBrST -- Amol Sharma Third Year Student Computer Science and Engineering MNNIT Allahabad

Re: [algogeeks] You have given a number N. You need to find out total number of set bits from 0 to N.

2011-09-11 Thread saurabh singh
Precomputation is the way to go On Sun, Sep 11, 2011 at 5:13 PM, ravi maggon wrote: > int bitcount (unsigned int n) { > int count = 0 ; > while (n) { > count++ ; > n = n & (n - 1) ; > } > return count ; > > } > > > On Sun, Sep 11, 2011 at 5:08 PM, ravi maggon wrote: > > > >

Re: [algogeeks] You have given a number N. You need to find out total number of set bits from 0 to N.

2011-09-11 Thread ravi maggon
int bitcount (unsigned int n) { int count = 0 ; while (n) { count++ ; n = n & (n - 1) ; } return count ; } On Sun, Sep 11, 2011 at 5:08 PM, ravi maggon wrote: > > I found this algo on http://stackoverflow.com/questions/109023/best-algorithm-to-count-the-number-of-set-bits-in-

Re: [algogeeks] You have given a number N. You need to find out total number of set bits from 0 to N.

2011-09-11 Thread ravi maggon
I found this algo on http://stackoverflow.com/questions/109023/best-algorithm-to-count-the-number-of-set-bits-in-a-32-bit-integer int NumberOfSetBits(int i) { i = i - ((i >> 1) & 0x); i = (i & 0x) + ((i >> 2) & 0x); return ((i + (i >> 4) & 0x0F0F0F0F) * 0x01010101)

[algogeeks] You have given a number N. You need to find out total number of set bits from 0 to N.

2011-09-11 Thread Neha Singh
-- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at ht