Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Bit-Manipulation/BinaryCountSetBits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }