Number of 1 bits

July 13, 2022

bit-manipulation

Problem URL: Number of 1 bits

We can logical and any number with 1 to check whether the last bit of that number is 1 or 0. We can also check this by checking the number is odd or even. Odd numbers always has last bit as 1. We count that bit and then write shift the number 1 bit to get the next bit. Finally after going through every bit, we return the result.

class Solution:
    def hammingWeight(self, n: int) -> int:
        count = 0
        while n:
            count += n & 1
            n >>= 1
        return count

Time Complexity: O(n)
Space Complexity: O(1)