From 1f3515b9856d234566107e041f779bb212a727e5 Mon Sep 17 00:00:00 2001 From: Rahul Bhandari <90493221+imrahulkb@users.noreply.github.com> Date: Sat, 14 Oct 2023 14:55:54 +0530 Subject: [PATCH 1/2] Update BinaryCountSetBits.js --- Bit-Manipulation/BinaryCountSetBits.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Bit-Manipulation/BinaryCountSetBits.js b/Bit-Manipulation/BinaryCountSetBits.js index e0a6d9414c..cb4c9dadc8 100644 --- a/Bit-Manipulation/BinaryCountSetBits.js +++ b/Bit-Manipulation/BinaryCountSetBits.js @@ -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 + var count = 0 + while (a) { + a &= (a - 1) + count++ + } + + return count; } export { BinaryCountSetBits } From db420775f60ac0551dd88086c58931ebd7995681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sat, 14 Oct 2023 14:03:26 +0200 Subject: [PATCH 2/2] Use `let` instead of `var` --- Bit-Manipulation/BinaryCountSetBits.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Bit-Manipulation/BinaryCountSetBits.js b/Bit-Manipulation/BinaryCountSetBits.js index cb4c9dadc8..b879f3bd67 100644 --- a/Bit-Manipulation/BinaryCountSetBits.js +++ b/Bit-Manipulation/BinaryCountSetBits.js @@ -14,13 +14,13 @@ function BinaryCountSetBits(a) { if (!Number.isInteger(a)) throw new TypeError('Argument not an Integer') - var count = 0 + let count = 0 while (a) { a &= (a - 1) count++ } - return count; + return count } export { BinaryCountSetBits }