法一:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;
int count=0;
scanf("%d",&number);
while(number){
if(number%2==1)
count++;
number/=2;
}
printf("%d\n",count);
}
法二:
<pre name="code" class="cpp">#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;
int count=0;
scanf("%d",&number);
while(number){
if(number&1)
count++;
number=number>>1;//如果只写number>>1,number本身不变
}
printf("%d\n",count);
}