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.