leetcode刷题,求hamming weight

最近看到一个不错的刷题网站leetCode,于是就去刷题啦,提高一下水平。题目真的是,很有代表性啊,我喜欢!

写一个函数,求出一个无符号整数里面位为1的个数(也就是Hamming Weight算法),例如32位整数11,用二进制表示为00000000000000000000000000001011,因此函数返回值为3

原文如下

Write a function that takes an unsigned integer and returns the number of ’1′ bits it has (also known as the Hamming weight).

For example, the 32-bit integer ’11′ has binary representation 00000000000000000000000000001011, so the function should return 3.

(更多…)