code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

191 Number Of 1 Bits

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const numberOf1Bits = n => {
  let count = 0
  while (n) {
    n = n & (n - 1)
    count += 1
  }
  return count
}

console.log(numberOf1Bits(11111111111111111111111111111101))

Created 2022-03-21T09:01:46+00:00 · Edit