diff --git a/src/algorithms/math/bits/README.md b/src/algorithms/math/bits/README.md index 31da17a4..98b6c4dd 100644 --- a/src/algorithms/math/bits/README.md +++ b/src/algorithms/math/bits/README.md @@ -38,12 +38,14 @@ This method is a combination of "Clear Bit" and "Set Bit" methods. #### isEven This method determines if the number provided is even. +It is based on the fact that odd numbers have their last +right bit to be set to 1. -``` -Number: 5 +```text +Number: 5 = 0b0101 isEven: false -Number: 4 +Number: 4 = 0b0100 isEven: true ``` @@ -108,18 +110,19 @@ inverting all of the bits of the number and adding 1 to it. #### Multiply Two Signed Numbers This method multiplies two signed integer numbers using bitwise operators. -This method is based on the following : +This method is based on the following facts: ```text -a * b can be written in the below formats +a * b can be written in the below formats: 0 if a is zero or b is zero or both a and b are zeroes 2a * (b/2) if b is even 2a * (b - 1)/2 + a if b is odd and positive 2a * (b + 1)/2 - a if b is odd and negative ``` -The advantage of this approach is that in each recursive step one of the operands reduces to half its original value. -Hence, the run time complexity is O(log b) where b is the operand that reduces to half on each recursive step. +The advantage of this approach is that in each recursive step one of the operands +reduces to half its original value. Hence, the run time complexity is `O(log(b)` where `b` is +the operand that reduces to half on each recursive step. > See [multiply.js](multiply.js) for further details. diff --git a/src/algorithms/math/bits/__test__/isEven.test.js b/src/algorithms/math/bits/__test__/isEven.test.js index a3f965e4..b21bbfb0 100644 --- a/src/algorithms/math/bits/__test__/isEven.test.js +++ b/src/algorithms/math/bits/__test__/isEven.test.js @@ -7,5 +7,13 @@ describe('isEven', () => { expect(isEven(-2)).toBe(true); expect(isEven(1)).toBe(false); expect(isEven(-1)).toBe(false); + expect(isEven(-3)).toBe(false); + expect(isEven(3)).toBe(false); + expect(isEven(8)).toBe(true); + expect(isEven(9)).toBe(false); + expect(isEven(121)).toBe(false); + expect(isEven(122)).toBe(true); + expect(isEven(1201)).toBe(false); + expect(isEven(1202)).toBe(true); }); }); diff --git a/src/algorithms/math/bits/__test__/multiply.test.js b/src/algorithms/math/bits/__test__/multiply.test.js index 80269b13..c2195704 100644 --- a/src/algorithms/math/bits/__test__/multiply.test.js +++ b/src/algorithms/math/bits/__test__/multiply.test.js @@ -12,5 +12,7 @@ describe('multiply', () => { expect(multiply(4, -2)).toBe(-8); expect(multiply(-4, -4)).toBe(16); expect(multiply(4, -5)).toBe(-20); + expect(multiply(2, 121)).toBe(242); + expect(multiply(121, 2)).toBe(242); }); }); diff --git a/src/algorithms/math/bits/isEven.js b/src/algorithms/math/bits/isEven.js index bfc3bef3..843b4d28 100644 --- a/src/algorithms/math/bits/isEven.js +++ b/src/algorithms/math/bits/isEven.js @@ -1,6 +1,6 @@ /** * @param {number} number - * @return bool + * @return {boolean} */ export default function isEven(number) { return (number & 1) === 0; diff --git a/src/algorithms/math/bits/multiply.js b/src/algorithms/math/bits/multiply.js index 4f2e8c76..4dd33a9d 100644 --- a/src/algorithms/math/bits/multiply.js +++ b/src/algorithms/math/bits/multiply.js @@ -1,27 +1,38 @@ +import multiplyByTwo from './multiplyByTwo'; import divideByTwo from './divideByTwo'; import isEven from './isEven'; -import multiplyByTwo from './multiplyByTwo'; /** - * FUNCTION DEFINITION - * multiply(a, b) = 0 if a is zero or b is zero or if both a and b are zeros - * multiply(a, b) = multiply(2a, b/2) if b is even - * multiply(a, b) = multiply(2a, (b-1)/2) + a if b is odd and b is positive - * multiply(a, b) = multiply(2a, (b+1)/2) - a if b is odd and b is negative + * Multiply two signed numbers using bitwise operations. + * + * If a is zero or b is zero or if both a and b are zeros: + * multiply(a, b) = 0 + * + * If b is even: + * multiply(a, b) = multiply(2a, b/2) + * + * If b is odd and b is positive: + * multiply(a, b) = multiply(2a, (b-1)/2) + a + * + * If b is odd and b is negative: + * multiply(a, b) = multiply(2a, (b+1)/2) - a + * + * Time complexity: O(log b) * - * COMPLEXITY - * O(log b) * @param {number} a * @param {number} b - * @return {number} a * b + * @return {number} */ export default function multiply(a, b) { + // If a is zero or b is zero or if both a and b are zeros then the production is also zero. if (b === 0 || a === 0) { return 0; } + // Otherwise we will have four different cases that are described above. const multiplyByOddPositive = () => multiply(multiplyByTwo(a), divideByTwo(b - 1)) + a; const multiplyByOddNegative = () => multiply(multiplyByTwo(a), divideByTwo(b + 1)) - a; + const multiplyByEven = () => multiply(multiplyByTwo(a), divideByTwo(b)); const multiplyByOdd = () => (b > 0 ? multiplyByOddPositive() : multiplyByOddNegative());