Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: adityahiran/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: feat-is-positive
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 2 commits
  • 4 files changed
  • 2 contributors

Commits on Sep 10, 2018

  1. Copy the full SHA
    584c5a9 View commit details

Commits on Sep 14, 2018

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    a800020 View commit details
23 changes: 23 additions & 0 deletions src/algorithms/math/bits/README.md
Original file line number Diff line number Diff line change
@@ -51,6 +51,29 @@ isEven: true

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

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

```text
Number: 1 = 0b0001
isPositive: true
Number: -1 = -0b0001
isPositive: false
Number: 0 = 0b0000
isPositive: false
Number: -0 = 0b0000
isPositive: false
```

> See [isPositive.js](isPositive.js) for further details.
#### Multiply By Two

This method shifts original number by one bit to the left.
10 changes: 10 additions & 0 deletions src/algorithms/math/bits/__test__/isPositive.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import isPositive from '../isPositive';

describe('isPositive', () => {
it('should detect if a number is positive', () => {
expect(isPositive(0)).toBe(false);
expect(isPositive(-0)).toBe(false);
expect(isPositive(1)).toBe(true);
expect(isPositive(-1)).toBe(false);
});
});
13 changes: 13 additions & 0 deletions src/algorithms/math/bits/isPositive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @param {number} number
* @return {boolean}
*/
export default function isPositive(number) {
// Zero is neither a positive nor a negative number
if (number === 0) {
return false;
}

// The most signification bit can be used to determine whether .
return ((number >> 31) & 1) === 0;
}
3 changes: 2 additions & 1 deletion src/algorithms/math/bits/multiply.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import multiplyByTwo from './multiplyByTwo';
import divideByTwo from './divideByTwo';
import isEven from './isEven';
import isPositive from './isPositive';

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

const multiplyByEven = () => multiply(multiplyByTwo(a), divideByTwo(b));
const multiplyByOdd = () => (b > 0 ? multiplyByOddPositive() : multiplyByOddNegative());
const multiplyByOdd = () => (isPositive(b) ? multiplyByOddPositive() : multiplyByOddNegative());

return isEven(b) ? multiplyByEven() : multiplyByOdd();
}