int fun(int x)
{
int countx = 0;
while (x)
{
countx++;
x = x & (x – 1);
}
return countx;
}
基本原理是:从右边起假设x的k位为1,其后面全是0,那么x-1的k位则为0,x&(x-1)后,k和k以后的所有位均变为0
int fun(int x)
{
int countx = 0;
while (x)
{
countx++;
x = x & (x – 1);
}
return countx;
}
基本原理是:从右边起假设x的k位为1,其后面全是0,那么x-1的k位则为0,x&(x-1)后,k和k以后的所有位均变为0