n & (n-1) can remove the last zero 1 in binary format.
class Solution(object):
def hammingWeight(self, n):
"""
:type n: int
:rtype: int
"""
res = 0
while n:
n = n & (n-1)
res += 1
return res