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

Commit92b9e6a

Browse files
committed
Add more tests to isPositive() bitwise function.
1 parentab7755a commit92b9e6a

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,16 @@ isEven: true
5353
5454
####isPositive
5555

56-
This method determines if the number provided is positive.
57-
It is based on the fact that all positive numbers have their last
58-
left bit to be set to 0. However, if the number provided is zero
59-
or negative zero, it should still return false.
56+
This method determines if the number is positive. It is based on the fact that all positive
57+
numbers have their leftmost bit to be set to`0`. However, if the number provided is zero
58+
or negative zero, it should still return`false`.
6059

6160
```text
6261
Number: 1 = 0b0001
6362
isPositive: true
6463
6564
Number: -1 = -0b0001
6665
isPositive: false
67-
68-
Number: 0 = 0b0000
69-
isPositive: false
70-
71-
Number: -0 = 0b0000
72-
isPositive: false
7366
```
7467

7568
>See[isPositive.js](isPositive.js) for further details.

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@ import isPositive from '../isPositive';
22

33
describe('isPositive',()=>{
44
it('should detect if a number is positive',()=>{
5+
expect(isPositive(1)).toBe(true);
6+
expect(isPositive(2)).toBe(true);
7+
expect(isPositive(3)).toBe(true);
8+
expect(isPositive(5665)).toBe(true);
9+
expect(isPositive(56644325)).toBe(true);
10+
511
expect(isPositive(0)).toBe(false);
612
expect(isPositive(-0)).toBe(false);
7-
expect(isPositive(1)).toBe(true);
813
expect(isPositive(-1)).toBe(false);
14+
expect(isPositive(-2)).toBe(false);
15+
expect(isPositive(-126)).toBe(false);
16+
expect(isPositive(-5665)).toBe(false);
17+
expect(isPositive(-56644325)).toBe(false);
918
});
1019
});
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
2-
*@param {number} number
2+
*@param {number} number - 32-bit integer.
33
*@return {boolean}
44
*/
55
exportdefaultfunctionisPositive(number){
6-
// Zero is neither a positive nor a negative number
6+
// Zero is neither a positive nor a negative number.
77
if(number===0){
88
returnfalse;
99
}
1010

11-
// The mostsignificationbit can be used to determine whether .
11+
// The mostsignificant 32ndbit can be used to determine whetherthe number is positive.
1212
return((number>>31)&1)===0;
1313
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp