判断一个数中二进制中1的个数

1.写一个函数返回参数二进制中 1 的个数
比如: 15 0000 1111 4 个 1
程序原型:
int count_one_bits(unsigned int value)
{
返回 1的位数
}



int count_one_bits(n)
{
int count = 0;
while (n)
{
n = n&(n - 1);
count++;
}
printf("%d ", count);
}
int main()
{
int a = 99;
//scanf("%d ", &a);
//int count = 0;
int ret = count_one_bits(a);
//while (a)
//{
// a = a&(a - 1);
// count++;
//}
//printf("%d ", count);
//int i = 0;


///*while (a)
//{
// if (a % 2 == 1)
// {
// count++;
// a = a / 2;
// }
//}
//printf("%d ", count);*///此方法不能计算负数
/*for (i = 0; i < 32; i++)
{
if (((a<<i)&1)== 1)
count++;
}
printf("%d ", count);*/
system("pause");
return 0;

}

结果截图:判断一个数中二进制中1的个数