求一个数二进制中1的个数

#include<stdio.h>
int main()
{
	int i,count=0;
	scanf("%d",&i);
	while(i)
	{
		if(i%2==1)
		{
			count++;
		}
		i=i/2;	
	}
	printf("%d\n",count);
	return 0;
}

//以调用函数来实现该程序
#include<stdio.h>
int count_bit(unsigned int value)
{
	int count=0;
	while(value)
	{
		count++;
		value=value&(value-1);     
	}                      
	return count;		           
}
int main()
{
	int ret=0;
	ret=count_bit(15);
	printf("%d\n",ret);
	return 0;
}



 
 
 
 
 

你可能感兴趣的:(printf)