mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Create multiply.js (#140)
Multiply Two numbers using Bit-wise Operator.
This commit is contained in:
parent
cc3a5bd353
commit
53c7143e07
14
src/algorithms/math/bits/multiply.js
Normal file
14
src/algorithms/math/bits/multiply.js
Normal file
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @param {number, number}
|
||||
* @return {number}
|
||||
*/
|
||||
export default function multiply(number1, number2) {
|
||||
var c = 0, result = 0;
|
||||
while(number2){
|
||||
if(number2&1)
|
||||
result += (number1 << c);
|
||||
c += 1;
|
||||
number2 >>= 1;
|
||||
}
|
||||
return result;
|
||||
}
|
Loading…
Reference in New Issue
Block a user