位运算的技巧
n&(n-1)等于消除最后一个1
def hammingWeight(self, n):
"""
:type n: int
:rtype: int
"""
#因为n-1是把n的二进制表示的最后一个1变为0,把其后的0变为1
#异或之后就是消除了最后一个1
res = 0
while n != 0:
n &= (n-1)
res += 1
return res