Isaac Emmanuel
15 Jun 2024•EasyC++ / Python / TypeScript

Number of 1 Bits

C++, Python and TypeScript solutions for Number of 1 Bits.

The Number of 1 Bits solutions are below.

Solution files

Python•number-of-1-bits/synced-solution.py
class Solution:
    def hammingWeight(self, n: int) -> int:
        count = 0
        while n:
            count += n & 1
            n >>= 1
        return count
C++•number-of-1-bits/synced-solution.cpp
class Solution {
public:
    int hammingWeight(uint32_t n) {
        int count = 0;
        while (n) {
            count += n & 1;
            n >>= 1;
        }
        return count;
    }
};
TypeScript•number-of-1-bits/synced-solution.ts
function hammingWeight(n: number): number {
    let count = 0;
    while (n) {
        count += n & 1;
        n >>>= 1;
    }
    return count;
}