int main()
{
unsigned int a = 0;
int count = 0;
printf("请输入要求的整数>>:");
scanf("%d", &a);
while (a)
{
if (a % 2)
{
count++;
}
a = a / 2;
}
printf("该数中二进制位为1的位数为%d",count);
return 0;
}
int main()
{
int num = -1;
int i = 0;
int count = 0;//计数
for (i = 0; i < 32; i++)
{
if ((num >> i) & 1)
count++;
}
printf("二进制中1的个数 = %d\n", count);
return 0;
}
相对最优算法:有几个1就运行几次
int main()
{
int a = 15;
int count = 0;
while (a)
{
a = a & (a - 1);
count++;
}
printf("%d", count);
return 0;
}
标签:count,return,int,++,原码,printf,main,整形,溢出
From: https://blog.51cto.com/u_16284214/7728882