it is easy
int count =0;
take input in num
while(num)
{
num=num&(num-1);
count ++;
}
printf("%d",count);
On 7/3/10, Dheeraj Jain <[email protected]> wrote:
> http://geeksforgeeks.org/?p=1176
>
> On Sat, Jul 3, 2010 at 9:17 PM, Dave <[email protected]> wrote:
>
>> Assuming that x is a 32 bit integer:
>>
>> n = ((x >> 1) & 0x55555555) + (x & 0x55555555)
>> n = ((n >> 2) & 0x33333333) + (n % 0x33333333)
>> n = ((n >> 4) & 0x0F0F0F0F) + (n & 0x0F0F0F0F)
>> n = ((n >> 8) & 0x00FF00FF) + (n & 0x00FF00FF)
>> n = ((n >>16) & 0x0000FFFF) + (n & 0x0000FFFF)
>>
>> n now is the number of bits set in x.
>>
>> Dave
>>
>>
>>
>>
>> On Jul 3, 11:27 am, jalaj jaiswal <[email protected]> wrote:
>> > is there any better way of finding number of 1's in binary of a number
>> other
>> > then below:
>> >
>> > #include<stdio.h>
>> > #include<stdlib.h>
>> > int main(){
>> > int n;
>> > printf("enter numb\n");
>> > scanf("%d",&n);
>> > int i=1;
>> > int count=0;
>> > for(int j=0;j<31;j++){
>> > if(n&(i<<j)){
>> > count++;
>> > }
>> > }
>> > printf("%d",count);
>> > system("pause");
>> > return 0;
>> >
>> > }
>> >
>> > --
>> > With Regards,
>> > Jalaj Jaiswal
>> > +919026283397
>> > B.TECH IT
>> > IIIT ALLAHABAD
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<algogeeks%[email protected]>
>> .
>> 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 [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
--
With regard,
Shrinivas
mca,NIT DURGAPUR
-------------------------
If you wanna succeed, you will find a way - else - you will find an excuse
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.