計算9999的二進制中含有多少個1

// 求數字的二進制含有多少個1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
int func(int num)
{
int countx = 0;
while (num)
{
countx ++;
num = num&(num-1);
}
return countx;
}
int _tmain(int argc, _TCHAR* argv[])
{
	int num=9999;
	int count=func(9999);
	printf("%d",count);
	getchar();
	return 0;
}

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