求一个数的二进制中有多少个一
求一个整数所对应的二进制中有多少个bit位为一
#include<stdio.h>
#include<stdlib.h>
int count_one_bits(unsigned int value)
{
int count = 0;for (int i = 0; i < 32; i++)
{
if (value & 1 == 1)
{
count++;
}
value = value >> 1;
}
return count;
}
int main()
{
unsigned int value= 0;
scanf("%d", &value);
int ret = count_one_bits(value);
printf("%d\n", ret);
system("pause");
return 0;
}
求一个整数所对应的二进制中有多少个bit位为一