編程之美---求二進制中1的個數

#include<stdio.h>

int bitCount(unsigned int b)
{
	int count = 0;
	while(b)
	{
		count += b & 0x00000001;
		b = b >> 1;
	}
	return count;
}

void test()
{
	unsigned int b;
	int ret = 0;
	while(scanf("%d", &b) != EOF)
	{
		ret = bitCount(b);
		printf("%d\n", ret);
	}
}

int main()
{
	test();
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章