1353. Subtract Using Bitwise Operators Only

MediumBit ManipulationBit ArithmeticXORBorrow

Given two integers a and b, return a - b using only bitwise operations. Subtraction mirrors addition, but the borrow is found by AND-ing the complement of a with b.

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

Output: Return a - b.

Examples

Example 1
Input: {"a":29,"b":13}
Output: 16
Explanation: Borrow propagation gives 16.
Example 2
Input: {"a":5,"b":9}
Output: -4
Explanation: The result is negative -> -4.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →