Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit de6a24e

Browse files
committedSep 8, 2018
Minor code style fixes for bitwise multiplication.
1 parent bc8943d commit de6a24e

File tree

5 files changed

+41
-17
lines changed

5 files changed

+41
-17
lines changed
 

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ This method is a combination of "Clear Bit" and "Set Bit" methods.
3838
#### isEven
3939

4040
This method determines if the number provided is even.
41+
It is based on the fact that odd numbers have their last
42+
right bit to be set to 1.
4143

42-
```
43-
Number: 5
44+
```text
45+
Number: 5 = 0b0101
4446
isEven: false
4547
46-
Number: 4
48+
Number: 4 = 0b0100
4749
isEven: true
4850
```
4951

@@ -108,18 +110,19 @@ inverting all of the bits of the number and adding 1 to it.
108110
#### Multiply Two Signed Numbers
109111

110112
This method multiplies two signed integer numbers using bitwise operators.
111-
This method is based on the following :
113+
This method is based on the following facts:
112114

113115
```text
114-
a * b can be written in the below formats
116+
a * b can be written in the below formats:
115117
0 if a is zero or b is zero or both a and b are zeroes
116118
2a * (b/2) if b is even
117119
2a * (b - 1)/2 + a if b is odd and positive
118120
2a * (b + 1)/2 - a if b is odd and negative
119121
```
120122

121-
The advantage of this approach is that in each recursive step one of the operands reduces to half its original value.
122-
Hence, the run time complexity is O(log b) where b is the operand that reduces to half on each recursive step.
123+
The advantage of this approach is that in each recursive step one of the operands
124+
reduces to half its original value. Hence, the run time complexity is `O(log(b)` where `b` is
125+
the operand that reduces to half on each recursive step.
123126

124127
> See [multiply.js](multiply.js) for further details.
125128

‎src/algorithms/math/bits/__test__/isEven.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,13 @@ describe('isEven', () => {
77
expect(isEven(-2)).toBe(true);
88
expect(isEven(1)).toBe(false);
99
expect(isEven(-1)).toBe(false);
10+
expect(isEven(-3)).toBe(false);
11+
expect(isEven(3)).toBe(false);
12+
expect(isEven(8)).toBe(true);
13+
expect(isEven(9)).toBe(false);
14+
expect(isEven(121)).toBe(false);
15+
expect(isEven(122)).toBe(true);
16+
expect(isEven(1201)).toBe(false);
17+
expect(isEven(1202)).toBe(true);
1018
});
1119
});

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

+2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ describe('multiply', () => {
1212
expect(multiply(4, -2)).toBe(-8);
1313
expect(multiply(-4, -4)).toBe(16);
1414
expect(multiply(4, -5)).toBe(-20);
15+
expect(multiply(2, 121)).toBe(242);
16+
expect(multiply(121, 2)).toBe(242);
1517
});
1618
});

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @param {number} number
3-
* @return bool
3+
* @return {boolean}
44
*/
55
export default function isEven(number) {
66
return (number & 1) === 0;

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

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
1+
import multiplyByTwo from './multiplyByTwo';
12
import divideByTwo from './divideByTwo';
23
import isEven from './isEven';
3-
import multiplyByTwo from './multiplyByTwo';
44

55
/**
6-
* FUNCTION DEFINITION
7-
* multiply(a, b) = 0 if a is zero or b is zero or if both a and b are zeros
8-
* multiply(a, b) = multiply(2a, b/2) if b is even
9-
* multiply(a, b) = multiply(2a, (b-1)/2) + a if b is odd and b is positive
10-
* multiply(a, b) = multiply(2a, (b+1)/2) - a if b is odd and b is negative
6+
* Multiply two signed numbers using bitwise operations.
7+
*
8+
* If a is zero or b is zero or if both a and b are zeros:
9+
* multiply(a, b) = 0
10+
*
11+
* If b is even:
12+
* multiply(a, b) = multiply(2a, b/2)
13+
*
14+
* If b is odd and b is positive:
15+
* multiply(a, b) = multiply(2a, (b-1)/2) + a
16+
*
17+
* If b is odd and b is negative:
18+
* multiply(a, b) = multiply(2a, (b+1)/2) - a
19+
*
20+
* Time complexity: O(log b)
1121
*
12-
* COMPLEXITY
13-
* O(log b)
1422
* @param {number} a
1523
* @param {number} b
16-
* @return {number} a * b
24+
* @return {number}
1725
*/
1826
export default function multiply(a, b) {
27+
// If a is zero or b is zero or if both a and b are zeros then the production is also zero.
1928
if (b === 0 || a === 0) {
2029
return 0;
2130
}
2231

32+
// Otherwise we will have four different cases that are described above.
2333
const multiplyByOddPositive = () => multiply(multiplyByTwo(a), divideByTwo(b - 1)) + a;
2434
const multiplyByOddNegative = () => multiply(multiplyByTwo(a), divideByTwo(b + 1)) - a;
35+
2536
const multiplyByEven = () => multiply(multiplyByTwo(a), divideByTwo(b));
2637
const multiplyByOdd = () => (b > 0 ? multiplyByOddPositive() : multiplyByOddNegative());
2738

0 commit comments

Comments
 (0)
Please sign in to comment.