您的位置:首页 > 其它

写一个函数返回参数值为1的个数 。如15 00001111 有4个1

2014-12-21 19:59 309 查看
#include <stdio.h>

int count_one_bits(unsigned int value)
{
int count = 0;
while(value)
{
if( value%2 == 1)
count++;
value = value/2;
}
printf("%d\n",count);
return 0;
}

int main()
{
unsigned int n;
scanf("%d",&n);
count_one_bits(n);
return 0;
}


#include <stdio.h>

int count_one_bits(unsigned int value)
{
int count = 0;
while(value)
{
count ++;
value = value & (value - 1) ;
}
printf("%d\n",count);
return 0;
}

int main()
{
unsigned int n;
scanf("%d",&n);
count_one_bits(n);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: