Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb9d0d9a

Browse files
committed
Add isPowerOfTwo function.
1 parent70b0e0a commitb9d0d9a

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
lines changed

‎src/algorithms/math/bits/README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,6 @@ inverting all of the bits of the number and adding 1 to it.
9191

9292
>See[switchSign.js](switchSign.js) for further details.
9393
94-
####Power of 2
95-
96-
This method checks if a number provided is power of two. It uses the property that when
97-
a power of 2 is`&` with power of 2 minus 1, it would return 0 implying that the provided
98-
number is power of 2.
99-
100-
```
101-
Number: 4
102-
Power of 2: True
103-
104-
Number: 1
105-
Power of 2: False
106-
```
107-
108-
>See`ifPowerof2` function for further details.
109-
110-
11194
####Multiply Two Numbers
11295

11396
This method multiplies two integer numbers using bitwise operators.
@@ -173,6 +156,24 @@ When we shift 1 four times it will become bigger than 5.
173156

174157
>See[bitLength.js](bitLength.js) for further details.
175158
159+
####Is Power of Two
160+
161+
This method checks if a number provided is power of two. It uses the following
162+
property. Let's say that`powerNumber` is a number that has been formed as a power
163+
of two (i.e. 2, 4, 8, 16 etc.). Then if we'll do`&` operation between`powerNumber`
164+
and`powerNumber - 1` it will return`0` (in case if number is power of two).
165+
166+
```
167+
Number: 4 = 0b0100
168+
Number: 3 = (4 - 1) = 0b0011
169+
4 & 3 = 0b0100 & 0b0011 = 0b0000 <-- Equal to zero, is power of two.
170+
171+
Number: 10 = 0b01010
172+
Number: 9 = (10 - 1) = 0b01001
173+
10 & 9 = 0b01010 & 0b01001 = 0b01000 <-- Not equal to zero, not a power of two.
174+
```
175+
176+
>See[isPowerOfTwo.js](isPowerOfTwo.js) for further details.
176177
177178
##References
178179

‎src/algorithms/math/bits/__test__/ifPowerOf2.test.js

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
importisPowerOfTwofrom'../isPowerOfTwo';
2+
3+
describe('isPowerOfTwo',()=>{
4+
it('should detect if the number is power of two',()=>{
5+
expect(isPowerOfTwo(1)).toBe(true);
6+
expect(isPowerOfTwo(2)).toBe(true);
7+
expect(isPowerOfTwo(3)).toBe(false);
8+
expect(isPowerOfTwo(4)).toBe(true);
9+
expect(isPowerOfTwo(5)).toBe(false);
10+
expect(isPowerOfTwo(6)).toBe(false);
11+
expect(isPowerOfTwo(7)).toBe(false);
12+
expect(isPowerOfTwo(8)).toBe(true);
13+
expect(isPowerOfTwo(9)).toBe(false);
14+
expect(isPowerOfTwo(16)).toBe(true);
15+
expect(isPowerOfTwo(23)).toBe(false);
16+
expect(isPowerOfTwo(32)).toBe(true);
17+
expect(isPowerOfTwo(127)).toBe(false);
18+
expect(isPowerOfTwo(128)).toBe(true);
19+
});
20+
});

‎src/algorithms/math/bits/ifPowerOf2.js

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
*@param {number} number
3+
*@return bool
4+
*/
5+
exportdefaultfunctionisPowerOfTwo(number){
6+
return(number&(number-1))===0;
7+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp