class Solution(object):
def hammingWeight(self, n):
"""
:type n: int
:rtype: int
"""
weight=0
while n>0:
weight+=n%2
n/=2
return weight
Simple Python Solution

@kid821654 use weight as the counter for bit 1.
n%2 will tell the value for lowest bit (2**0
), in which case you can considerweight+=n%2
asweight=weight if lastBit==0 else weight+1
check on the higher bit2**1
and repeat. Hope this helps