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 <http://gplus.to/amolsharma99> <http://twitter.com/amolsharma99><http://in.linkedin.com/pub/amol-sharma/21/79b/507><http://youtube.com/amolsharma99> On Sun, Sep 11, 2011 at 5:39 PM, saurabh singh <saurab...@gmail.com> wrote: > Precomputation is the way to go > > > On Sun, Sep 11, 2011 at 5:13 PM, ravi maggon <maggonr...@gmail.com> 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 <maggonr...@gmail.com> >> wrote: >> > >> > 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) & 0x55555555); >> > i = (i & 0x33333333) + ((i >> 2) & 0x33333333); >> > return ((i + (i >> 4) & 0x0F0F0F0F) * 0x01010101) >> 24; >> > } >> > >> > Can anyone explain this? >> > >> > >> > On Sun, Sep 11, 2011 at 5:04 PM, Neha Singh <neha.ndelhi.1...@gmail.com> >> wrote: >> > > >> > > -- >> > > 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 >> > > http://groups.google.com/group/algogeeks?hl=en. >> > > >> > >> > >> > >> > -- >> > Regards >> > Ravi Maggon >> > B.E. CSE, Final Year >> > Thapar University >> > >> > www.algorithmguru.com >> > >> > "Failure is the opportunity to begin again more intelligently" >> > >> > >> >> >> >> -- >> Regards >> Ravi Maggon >> B.E. CSE, Final Year >> Thapar University >> >> www.algorithmguru.com >> >> "Failure is the opportunity to begin again more intelligently" >> >> -- >> 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 >> http://groups.google.com/group/algogeeks?hl=en. >> > > > > -- > Saurabh Singh > B.Tech (Computer Science) > MNNIT ALLAHABAD > > > -- > 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 > http://groups.google.com/group/algogeeks?hl=en. > -- 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 http://groups.google.com/group/algogeeks?hl=en.