chore: count set bits using bitwise ops (#1532)

* Update BinaryCountSetBits.js

* Use `let` instead of `var`

---------

Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
Rahul Bhandari
2023-10-14 22:24:11 +05:30
committed by GitHub
parent 628c5aeb5c
commit 46362e3d47

View File

@ -14,8 +14,13 @@ function BinaryCountSetBits(a) {
if (!Number.isInteger(a)) throw new TypeError('Argument not an Integer')
// convert number into binary representation and return number of set bits in binary representation
return a.toString(2).split('1').length - 1
let count = 0
while (a) {
a &= (a - 1)
count++
}
return count
}
export { BinaryCountSetBits }