Sum of Two Integers

MediumBit Manipulation

Given two integers a and b, return their sum without using the operators + and - (this includes +=, -=, ++ and --).

Constraints

  • -1000 <= a, b <= 1000

Examples

Example 1

Input: a = 3, b = 5
Output: 8

011 + 101: the bits carry all the way up and the result is 1000, which is 8.

Example 2

Input: a = -7, b = 4
Output: -3

Negative numbers are stored in two's complement, so the same loop gives -3.

Example 3

Input: a = -1, b = 1
Output: 0

-1 is all 1 bits. The carry ripples through every bit and falls off the top, leaving 0.

JavaScript you'll use: Bitwise operators Numbers, Math and integer tricks

/**
 * @param {number} a
 * @param {number} b
 * @return {number}
 */
function getSum(a, b) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.