Last active
August 14, 2021 15:22
-
-
Save anish000kumar/fdec2dcccf35f2fe4c2720b95abad01c to your computer and use it in GitHub Desktop.
Binary Sum & multiply
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sum(x, y){ | |
if(y === 0) return x | |
return sum( x^y, (x&y)<<1 ) | |
} | |
function multiply(x, y){ | |
if(x ===0 || y ===0) return 0 | |
let ans = 0 | |
let shifts = 0 | |
while(x){ | |
if(x & 1) ans = sum(ans, y << shifts) | |
shifts++ | |
x = x >> 1 | |
} | |
return ans | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment