1352. Add Two Numbers Without Arithmetic Operators

MediumBit ManipulationBit ArithmeticXORCarry

Given two integers a and b, return their sum without using the + or - operators. Use XOR to add the bits that do not carry and AND with a shift to propagate the carry.

Input: A JSON object {"a": <integer>, "b": <integer>}.

Output: Return a + b.

Examples

Example 1
Input: {"a":13,"b":29}
Output: 42
Explanation: XOR and carry propagation give 42.
Example 2
Input: {"a":-7,"b":3}
Output: -4
Explanation: Two's complement handles the negative operand -> -4.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →