Created
May 11, 2015 13:17
-
-
Save danibrear/94bf8534bb0f594beae0 to your computer and use it in GitHub Desktop.
Just wanted to play around with creating addition and subtraction methods without using + and -
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
/* | |
Just wanted to play around with creating addition and subtraction methods without using + and - | |
@author: David Brear | |
@date: 2015-05-11 | |
*/ | |
var add = function (x, y) { | |
while (y !== 0) { | |
var carry = x & y; | |
x = x ^ y; | |
y = carry << 1; | |
} | |
return x; | |
}; | |
var subtract = function (x, y) { | |
var negy = add(~y, 1); | |
return add(x, negy); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment