Files
JavaScript/Bit-Manipulation/BinaryCountSetBits.js
Omkarnath Parida 9a875264cc prettier fixes & added test cases for Project Euler problem 4 (#1566)
* 📦 NEW: Added solution for ProjectEuler-007

* 🐛 FIX: Spelling mistake fixes

* 👌 IMPROVE: changed variable name from `inc` to `candidateValue` and thrown error in case of invalid input

* 👌 IMPROVE: Modified the code

* 👌 IMPROVE: Added test case for ProjectEuler Problem001

* 👌 IMPROVE: Added test cases for Project Euler Problem 4

* 👌 IMPROVE: auto prettier fixes

---------

Co-authored-by: Omkarnath Parida <omkarnath.parida@yocket.in>
2023-10-24 19:19:37 +02:00

27 lines
664 B
JavaScript

/*
author: vivek9patel
license: GPL-3.0 or later
This script will find number of 1's
in binary representation of given number
*/
function BinaryCountSetBits(a) {
'use strict'
// 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
if (!Number.isInteger(a)) throw new TypeError('Argument not an Integer')
let count = 0
while (a) {
a &= a - 1
count++
}
return count
}
export { BinaryCountSetBits }