Merge branch 'TheAlgorithms:master' into issues/720

This commit is contained in:
Eric L
2021-10-09 14:52:17 +02:00
committed by GitHub
2 changed files with 7 additions and 5 deletions

View File

@ -23,6 +23,8 @@
*/ */
export const IsPowerOfTwo = (n) => { export const IsPowerOfTwo = (n) => {
if (n != 0 && (n & (n - 1)) == 0) return true if (n > 0 && (n & (n - 1)) === 0) {
else return false return true
}
return false
} }

View File

@ -5,9 +5,9 @@ test('Check if 0 is a power of 2 or not:', () => {
expect(res).toBe(false) 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) const res = IsPowerOfTwo(1)
expect(res).toBe(false) expect(res).toBe(true)
}) })
test('Check if 4 is a power of 2 or not:', () => { test('Check if 4 is a power of 2 or not:', () => {