一种快速找到一个数中二进制表示1的数量的方法

在别人的代码中看到的,看来还是有一点作用的,代码如下:
#include 
using namespace std;

int quickfindonecnt(int i)
{
	int cnt = 0;
	while (i)
	{
		cnt++;
		i &= i-1;
	}
	return cnt;
}

int main()
{
	int i = 0xFFF;
	cout << quickfindonecnt(i) << endl;
	return 0;
}

posted on 2011-12-22 22:41  小橋流水  阅读(483)  评论(0编辑  收藏  举报

导航