int fun(x)
{
int count = 0;
while(x)
{
count ++;
x = x&(x-1);
}
return count;
}
假定x = 9999。 答案:8
将x转化为二进制数10011100001111
然后进行按位&运算
eg1:
x = 01001000
x-1 = 01000111
x&(x-1)=01000000
eg2:
x = 01001001
x-1 = 01001000
x&(x-1)=01001000