題目:
For example, the 32-bit integer ’11' has binary representation00000000000000000000000000001011, so the function should return 3.
代碼:
<span style="font-size:14px;">class Solution {
public:
int hammingWeight(uint32_t n) {
int num=0;
while(n!=0)
{
if(n&1==1)
num++;
n=n/2;
}
return num;
}
};
</span>
結果: