C語言快速統計其二進制形式中1的個數

    基於文章 http://blog.csdn.net/luoluoxiaocainiao/article/details/11534653   的補充

   就直接貼代碼吧!

  

#include <stdio.h>

int main()
{
	const int bits = sizeof(int)*8;
	int n,x;
	scanf("%d",&n);
	while (n--)
	{
		scanf("%d",&x);
		int cnt=0;
		for(int i=0; i<bits; i++)
		{
            cnt+=x<0?1:0;
			x <<= 1;
		}
		printf("%d\n",cnt);
	}
	return 0;
}


 

發佈了36 篇原創文章 · 獲贊 12 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章