The Number of 1 Bits solutions are below.
15 Jun 2024•Easy•C++ / Python / TypeScript
Number of 1 Bits
C++, Python and TypeScript solutions for Number of 1 Bits.
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;
}