Created
February 25, 2017 21:16
-
-
Save arturparkhisenko/b3ddfdad985035f73a98a2ff521a9245 to your computer and use it in GitHub Desktop.
js-sum-without-a-plus
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
/** | |
* @param {number} a | |
* @param {number} b | |
* @return {number} | |
*/ | |
var getSum = function(a, b) { | |
var result = a; | |
var rest = b; | |
while (rest) { | |
var carry = result & rest; // and (to get carry value: 0 || 1) | |
result = result ^ rest; // xor (real sum) | |
rest = carry << 1; //carry shift (increase by multiplying, same as: carry * 2, thats why we have a +1 effect here) | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment