Return the number of set bits (1s) in the binary representation of the positive integer n.
n & (n − 1) clears the lowest set bit. Count how many times you can do that before n becomes 0.
1function hammingWeight(n: number): number {2let count = 0;3while (n !== 0) {4n &= n - 1;5count++;6}7return count;8}
count = 0.
Space: play/pause · ←/→: step