Skip to content

Commit 92b9e6a

Browse files
committedSep 14, 2018
Add more tests to isPositive() bitwise function.
1 parent ab7755a commit 92b9e6a

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed
 

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

+3-10
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

+10-1
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
});
+3-3
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
export default function isPositive(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
return false;
99
}
1010

11-
// The most signification bit can be used to determine whether .
11+
// The most significant 32nd bit can be used to determine whether the number is positive.
1212
return ((number >> 31) & 1) === 0;
1313
}

0 commit comments

Comments
 (0)
Please sign in to comment.