MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / BinaryCountSetBits

Function BinaryCountSetBits

Bit-Manipulation/BinaryCountSetBits.js:10–24  ·  view source on GitHub ↗
(a)

Source from the content-addressed store, hash-verified

8*/
9
10function BinaryCountSetBits(a) {
11 'use strict'
12
13 // check whether input is an integer, some non-integer number like, 21.1 have non-terminating binary expansions and hence their binary expansion will contain infinite ones, thus the handling of non-integers (including strings,objects etc. as it is meaningless) has been omitted
14
15 if (!Number.isInteger(a)) throw new TypeError('Argument not an Integer')
16
17 let count = 0
18 while (a) {
19 a &= a - 1
20 count++
21 }
22
23 return count
24}
25
26export { BinaryCountSetBits }

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected