Hi Don,

instead of searching 1 at all places we can use this :

while(n!=0){
count++;
n = n & n-1;
}


On Fri, May 17, 2013 at 6:55 PM, Don <dondod...@gmail.com> wrote:

> Counting the set bits in one integer is not the problem which was
> asked.
> However, I think that something like this is both faster and more easy
> to understand than what you have written:
>
> int bitCount(unsigned int x)
> {
>    int result = 0;
>    while(x)
>    {
>       if (x & 1) ++result;
>       x >>= 1;
>    }
>    return result;
> }
>
> On May 17, 8:32 am, bhargav <ybbkris...@gmail.com> wrote:
> > as bitwise operators are fast can count by following logic, works oly fr
> > +ve, just a tweak will make it to work with -ves also ..
> >
> > #include <stdio.h>
> > main() {
> > unsigned int x=12312,a;
> > a=x<<1;
> > //printf("%u",a);
> > int count=0;
> > while(x>0) {
> > a = x<<1;
> > //printf("%u \n",a);
> > if(a<x)
> > count++;
> > x=a;}
> >
> > printf("%d\n",count );
> > getch();
> >
> >
> >
> >
> >
> >
> >
> > }
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to algogeeks+unsubscr...@googlegroups.com.
>
>
>


-- 
Mangal Dev
Ph No. 7411627654
Member Technical Staff
Oracle India Pvt Ltd
mangal....@oracle.com <mangal.d...@oracle.com>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to algogeeks+unsubscr...@googlegroups.com.


Reply via email to