Skip to content

Commit e978f43

Browse files
Add code and tests for checking power of 2
1 parent cbf28a7 commit e978f43

File tree

3 files changed

+680
-82
lines changed

3 files changed

+680
-82
lines changed

Bit-Manipulation/IsPowerOfTwo.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
author: @Aayushi-Mittal
3+
4+
This script will check whether the given
5+
number is a power of two or not.
6+
7+
*/
8+
9+
export const IsPowerOfTwo = (n) => {
10+
if ((n&(n-1))==0 && n!=0)
11+
return true;
12+
else
13+
return false;
14+
}
15+
16+
// console.log(IsPowerOfTwo(0));
17+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {IsPowerOfTwo} from '../IsPowerOfTwo'
2+
3+
test('Check if 0 is a power of 2 or not:', () => {
4+
const res = IsPowerOfTwo(1, 0)
5+
expect(res).toBe(false)
6+
})
7+
8+
test('Check if 4 is a power of 2 or not:', () => {
9+
const res = IsPowerOfTwo(4)
10+
expect(res).toBe(true)
11+
})
12+
13+
test('Check if 1024 is a power of 2 or not:', () => {
14+
const res = IsPowerOfTwo(1024)
15+
expect(res).toBe(true)
16+
})
17+
18+
test('Check if 1025 is a power of 2 or not:', () => {
19+
const res = IsPowerOfTwo(1025)
20+
expect(res).toBe(false)
21+
})

0 commit comments

Comments
 (0)