From 584c5a90577efafc3fc6499f9556a9389e4b58c9 Mon Sep 17 00:00:00 2001
From: adityahiran
Date: Mon, 10 Sep 2018 14:13:40 -0400
Subject: [PATCH] feat(bitwise): Function to check if a number is positive
---
src/algorithms/math/bits/README.md | 23 +++++++++++++++++++
.../math/bits/__test__/isPositive.test.js | 10 ++++++++
src/algorithms/math/bits/isPositive.js | 13 +++++++++++
src/algorithms/math/bits/multiply.js | 3 ++-
4 files changed, 48 insertions(+), 1 deletion(-)
create mode 100644 src/algorithms/math/bits/__test__/isPositive.test.js
create mode 100644 src/algorithms/math/bits/isPositive.js
diff --git a/src/algorithms/math/bits/README.md b/src/algorithms/math/bits/README.md
index 98b6c4dd2d..e03097514a 100644
--- a/src/algorithms/math/bits/README.md
+++ b/src/algorithms/math/bits/README.md
@@ -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.
diff --git a/src/algorithms/math/bits/__test__/isPositive.test.js b/src/algorithms/math/bits/__test__/isPositive.test.js
new file mode 100644
index 0000000000..02e351c812
--- /dev/null
+++ b/src/algorithms/math/bits/__test__/isPositive.test.js
@@ -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);
+ });
+});
diff --git a/src/algorithms/math/bits/isPositive.js b/src/algorithms/math/bits/isPositive.js
new file mode 100644
index 0000000000..5be23d359f
--- /dev/null
+++ b/src/algorithms/math/bits/isPositive.js
@@ -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;
+}
diff --git a/src/algorithms/math/bits/multiply.js b/src/algorithms/math/bits/multiply.js
index 4dd33a9d14..da952f429e 100644
--- a/src/algorithms/math/bits/multiply.js
+++ b/src/algorithms/math/bits/multiply.js
@@ -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();
}