【leetcode】【191】Number of 1 Bits

#include<iostream>
#include<stdint.h>
using namespace std;

class Solution {
public:
	int hammingWeight(uint32_t n) {
		int count = 0;
		while (n){
			n &= (n - 1);//见http://blog.csdn.net/ruan875417/article/details/43018069
			++count;
		}
		return count;
	}
};

int main(){
	Solution solution;
	cout << solution.hammingWeight(11) << endl;

	system("pause");
	return 0;
}

posted on 2015-05-09 10:50  ruan875417  阅读(121)  评论(0编辑  收藏  举报

导航