Number of 1 Bits

 

191. Number of 1 Bits

Solution

def hammingWeight(self, n):
    """
    :type n: int
    :rtype: int
    """
    res = 0
    while n != 0:
        tmp = n
        n = n >> 1
        if n << 1 != tmp:
            res += 1
            
    return res