code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

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