
Leetcode Problem 191: Number of 1 Bits
Given a positive integer n, return the number of set bits (bits with value 1) in its 32-bit binary representation. This count is also known as the Hamming weight or popcount.
For example, integer 11 has binary representation 00000000000000000000000000001011, which contains 3 set bits.
Follow up: If this function is called many times, how would you optimize it?
Constraints:
function hammingWeight(n: number): number {
let number = 0
for (let i = 0; i < 32; i++) {
number = number + (n & 1)
n = n >>> 1
}
return number
}
console.log(hammingWeight(2147483645))