Last active
December 14, 2015 20:09
-
-
Save arian/5142169 to your computer and use it in GitHub Desktop.
How Hardware multiplies unsigned integers
This file contains hidden or 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 multiply(multiplicant, multiplier){ | |
var product = 0; | |
for (var i = 0; i < 32; i++){ | |
product += (multiplier & 1) ? multiplicant : 0; | |
multiplicant <<= 1; | |
multiplier >>= 1; | |
} | |
return product; | |
} | |
console.log(multiply(2, 3)); | |
console.log(multiply(2, 4)); | |
console.log(multiply(2, 5)); | |
console.log(multiply(30, 5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment