I know an alternative to using the +
sign for addition is to do something like this:
int add(int a, int b)
{
if(b == 0)
return sum;
sum = a ^ b;
carry = (a & b) << 1;
return add(sum,carry);
}
But I have two problems:
- This is C++, not JavaScript. Is this supported in JavaScript?
- It's obvious the whole trick is in
^
&
<<
, but I don't know how to start looking for them in JavaScript, because I don't know what they are called. What should I be googling for even?
I tried to write this in JavaScript ... but seems I miss something
var getSum = function(a, b) {
return (a ^ b, (a & b) << 1)
};