回复 楼主 冬青123
//兄弟,你描述的问题不是很清楚。如果你想要得到一个整数中的位中为1的个数的话。代码如下,如果不是,你可能要重新描述一下你的问题。
#include <stdio.h>
int binaryon_count(int n);
int main(void)
{
int test;
printf("请输入一个整数:\n");
scanf("%d",&test);
printf("%d 的位中为1的数量是%d 个。\n",test,binaryon_count(test));
printf("谢谢使用!\n");
return 0;
}
int binaryon_count(int n)
{
int count=0;
while(n)
{
if(n%2)
count++;
n/=2;
}
return count;
}