Question: https://leetcode.com/problems/number-of-1-bits/
Question Name: Number of 1 Bits
The key is how to find and remove the last 1-bit.
1 2 3 4 5 6 7 8 9 | class Solution: # @param n, an integer # @return an integer def hammingWeight(self, n): count = 0 while n != 0: count += 1 n = n & (n - 1) # Remove the last 1-bit. return count |