#include<stdio.h>
int main()
{
int num = -1, count = 0;
while (num)
{
count++;
num = num&(num - 1);
}
printf("%d\n", count);
system("pause");
return 0;
}
用函数编写
#include<stdio.h>
int count_one_bit(num)
{
int count = 0;
while (num)
{
count++;
num = num&(num - 1);
}
return count;
}
int main()
{
int num = -1;
int ret = count_one_bit(num);
printf("%d\n", ret);
system("pause");
return 0;
}