/*版一*/
unsigned int bitcount(unsigned int x)
{
int b;
for(b=0;x != 0;x >>= 1)
{
if(x&01)
b++;
}
return b;
}
![](https://i-blog.csdnimg.cn/blog_migrate/217e2f2a44a49cfbb3f217a7e1d5a25b.gif)
unsigned int bitcount(unsigned int x)
{
int b;
for(b=0;x != 0;)
{
x &= (x-1); /* 这里的做用是每次都会在原的的数的基础上去掉一个1,贼酷 */
b++;
}
return b;
}