一、计算一个int整形数转化为2进制后1的个数
#include <stdio.h>
int func(int x){
int count = 0;
while(x)
{
count++;
printf("x=%d\n",x);
x=x&(x-1);
}
return count;
}
int main(){
int i = func(9999);
printf("the count=%d\n",i);
}
9999转化为2进制后是:10011100001111
所以以上到输出是:
x=9999
x=9998
x=9996
x=9992
x=9984
x=9728
x=9216
x=8192
the count=8