Back to all solutions

#371 - Sum of Two Integers

Problem Description

Given two integers a and b, return the sum of the two integers without using the operators + and -.

Solution

/**
 * @param {number} a
 * @param {number} b
 * @return {number}
 */
var getSum = function(a, b) {
  const sum = a ^ b;
  const carry = (a & b) << 1;

  if (!carry) {
    return sum;
  }

  return getSum(sum, carry);
};