Skip to content

Commit 18aca1c

Browse files
authored
Create multiplication.js
1 parent dcfaadf commit 18aca1c

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

multiplication.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function multiplyByAdd(a, b) {
2+
let product = 0;
3+
while (b>0) {
4+
product += a; // Keep a running sum
5+
b--; // Count down using b
6+
}
7+
return product;
8+
}
9+
10+
function multiplyByShift(a, b) {
11+
let product = 0;
12+
while (b>0) {
13+
if (b & 0b000001) {
14+
product += a;
15+
}
16+
b >>= 1; // Consider the next bit of b
17+
a <<= 1; // Double a to match place
18+
}
19+
return product;
20+
}
21+

0 commit comments

Comments
 (0)