We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 628c5ae commit 46362e3Copy full SHA for 46362e3
Bit-Manipulation/BinaryCountSetBits.js
@@ -14,8 +14,13 @@ function BinaryCountSetBits(a) {
14
15
if (!Number.isInteger(a)) throw new TypeError('Argument not an Integer')
16
17
- // convert number into binary representation and return number of set bits in binary representation
18
- return a.toString(2).split('1').length - 1
+ let count = 0
+ while (a) {
19
+ a &= (a - 1)
20
+ count++
21
+ }
22
+
23
+ return count
24
}
25
26
export { BinaryCountSetBits }
0 commit comments