Skip to content

Instantly share code, notes, and snippets.

@danibrear
Created May 11, 2015 13:17
Show Gist options
  • Save danibrear/94bf8534bb0f594beae0 to your computer and use it in GitHub Desktop.
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 -
/*
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