Dodomeki
Sign in

132. Register Adjustment

Medium

A register stores signed 32-bit integer values. Given the integers a and b, compute their sum without using addition or subtraction operators.

The result must follow the signed 32-bit representation: if the sum exceeds the representable range, discard the excess bits. Return the resulting value interpreted as a signed 32-bit integer.

Example 1
Input: a = 7, b = 4
Output: 11
Explanation: The sum fits in the 32-bit range.
Example 2
Input: a = 2147483647, b = 1
Output: -2147483648
Explanation: The result overflows and wraps to the smallest signed 32-bit integer.
Example 3
Input: a = -12, b = 5
Output: -7
Explanation: Adding a negative value and a positive value produces -7.
Constraints
  • -2^31 <= a <= 2^31 - 1
  • -2^31 <= b <= 2^31 - 1
BitsMath
Caso 1
a =
7
b =
4
Expected =
11
Caso 2
a =
2147483647
b =
1
Expected =
-2147483648
Caso 3
a =
-12
b =
5
Expected =
-7

Run checks these 3 cases. Submit runs all 12, including hidden ones.