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.
解法1:O(M) M is number of total set bits
运用n & (n -1)消掉最高位的set bit的思想,可以不停重复这个操作直到n变为0,
这样的复杂度可以减少为所有1的个数
C++
解法2:O(N) N is number of total bits
用右移的方法来判断每一位是否是1直到n变为0
C++
Java