Problem Statement in English
Approach
This is a subset of this problem.
Solution in Python
class Solution:
def hammingWeight(self, n: int) -> int:
ans = 0
while n > 0:
if n&1 > 0:
ans+=1
n = n >> 1
return ans
Complexity
Time: $O(1)$
Since we only process $32$ bits at most.Space: $O(1)$
Since we only store an integer variable.
And we are done.