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

Commitab7755a

Browse files
adityahirantrekhleb
authored andcommitted
feat(bitwise): Function to check if a number is positive (trekhleb#204)
1 parent861e0e9 commitab7755a

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,29 @@ isEven: true
5151

5252
>See[isEven.js](isEven.js) for further details.
5353
54+
####isPositive
55+
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.
60+
61+
```text
62+
Number: 1 = 0b0001
63+
isPositive: true
64+
65+
Number: -1 = -0b0001
66+
isPositive: false
67+
68+
Number: 0 = 0b0000
69+
isPositive: false
70+
71+
Number: -0 = 0b0000
72+
isPositive: false
73+
```
74+
75+
>See[isPositive.js](isPositive.js) for further details.
76+
5477
####Multiply By Two
5578

5679
This method shifts original number by one bit to the left.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
importisPositivefrom'../isPositive';
2+
3+
describe('isPositive',()=>{
4+
it('should detect if a number is positive',()=>{
5+
expect(isPositive(0)).toBe(false);
6+
expect(isPositive(-0)).toBe(false);
7+
expect(isPositive(1)).toBe(true);
8+
expect(isPositive(-1)).toBe(false);
9+
});
10+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
*@param {number} number
3+
*@return {boolean}
4+
*/
5+
exportdefaultfunctionisPositive(number){
6+
// Zero is neither a positive nor a negative number
7+
if(number===0){
8+
returnfalse;
9+
}
10+
11+
// The most signification bit can be used to determine whether .
12+
return((number>>31)&1)===0;
13+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
importmultiplyByTwofrom'./multiplyByTwo';
22
importdivideByTwofrom'./divideByTwo';
33
importisEvenfrom'./isEven';
4+
importisPositivefrom'./isPositive';
45

56
/**
67
* Multiply two signed numbers using bitwise operations.
@@ -34,7 +35,7 @@ export default function multiply(a, b) {
3435
constmultiplyByOddNegative=()=>multiply(multiplyByTwo(a),divideByTwo(b+1))-a;
3536

3637
constmultiplyByEven=()=>multiply(multiplyByTwo(a),divideByTwo(b));
37-
constmultiplyByOdd=()=>(b>0 ?multiplyByOddPositive() :multiplyByOddNegative());
38+
constmultiplyByOdd=()=>(isPositive(b) ?multiplyByOddPositive() :multiplyByOddNegative());
3839

3940
returnisEven(b) ?multiplyByEven() :multiplyByOdd();
4041
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp