Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Bit-Manipulation/IsPowerOfTwo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
_______
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions Bit-Manipulation/test/IsPowerOfTwo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:', () => {
Expand Down