写一个函数返回参数二进制中 1 的个数
程序员文章站
2022-03-08 15:33:28
...
#include"stdio.h"
#include"stdlib.h"
#pragma warning(disable:4996)
int my_count(int data)
{
int i = 0;
int count = 0;
for (i = 0; i < 32; i++)
{
if ((data>>i)&0x1)
{
count++;
}
}
return count;
}
int main()
{
int value = 0;
printf("please input a number: ");
scanf("%d",&value);
printf("bit counts: %d\n",my_count(value));
system("pause");
return 0;
}