Number of 1 Bits

EasyDivide and ConquerBit Manipulation

Solution

export function hammingWeight(n: number): number {
  let bitCount = 0;
  while (n !== 0) {
    n &= n - 1;
    bitCount += 1;
  }
  return bitCount;
}