Devin's Blog

Home

❯

Coding

❯

Number of 1 Bits

Number of 1 Bits

Jan 23, 20231 min read

  • idea
class Solution:
    def hammingWeight(self, n: int) -> int:
        count = 0
        while True:
            remainder = n % 2
            n //= 2
            
            if remainder == 1:
                count += 1
 
            if n == 0:
                break
 
        return count

References

https://leetcode.com/problems/number-of-1-bits/description/


Graph View

Created with Quartz v4.5.0 © 2026

  • GitHub
  • Discord Community