From 8812f63ba281c4585a16495ba0072c3e38d49511 Mon Sep 17 00:00:00 2001 From: Eric Lavault <39483232+lvlte@users.noreply.github.com> Date: Sat, 9 Oct 2021 12:23:49 +0200 Subject: [PATCH] Small fixes + comply with JS standard styles. --- Bit-Manipulation/IsPowerOfTwo.js | 8 +++++--- Bit-Manipulation/test/IsPowerOfTwo.test.js | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Bit-Manipulation/IsPowerOfTwo.js b/Bit-Manipulation/IsPowerOfTwo.js index cbaf05f78..ec5bbcd17 100644 --- a/Bit-Manipulation/IsPowerOfTwo.js +++ b/Bit-Manipulation/IsPowerOfTwo.js @@ -14,7 +14,7 @@ And we know that 1's complement is just opp. of that number. So, (n & (n-1)) will be 0. - For eg: (1000 & (1000-1)) + For eg: (1000 & (1000-1)) 1 0 0 0 // Original Number (8) 0 1 1 1 // After Subtracting 1 (8-1 = 7) _______ @@ -23,6 +23,8 @@ */ export const IsPowerOfTwo = (n) => { - if (n != 0 && (n & (n - 1)) == 0) return true - else return false + if (n > 0 && (n & (n - 1)) === 0) { + return true + } + return false } diff --git a/Bit-Manipulation/test/IsPowerOfTwo.test.js b/Bit-Manipulation/test/IsPowerOfTwo.test.js index a971475f7..2ad0f3742 100644 --- a/Bit-Manipulation/test/IsPowerOfTwo.test.js +++ b/Bit-Manipulation/test/IsPowerOfTwo.test.js @@ -5,9 +5,9 @@ test('Check if 0 is a power of 2 or not:', () => { expect(res).toBe(false) }) -test('Check if 0 is a power of 2 or not:', () => { +test('Check if 1 is a power of 2 or not:', () => { const res = IsPowerOfTwo(1) - expect(res).toBe(false) + expect(res).toBe(true) }) test('Check if 4 is a power of 2 or not:', () => {