Skip to content

Instantly share code, notes, and snippets.

@anish000kumar
Last active August 14, 2021 15:22
Show Gist options
  • Save anish000kumar/fdec2dcccf35f2fe4c2720b95abad01c to your computer and use it in GitHub Desktop.
Save anish000kumar/fdec2dcccf35f2fe4c2720b95abad01c to your computer and use it in GitHub Desktop.
Binary Sum & multiply
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