📘
Sum of Two Integers
MediumBit Manipulation
Given two integers a and b, return their sum without using the + or - operators.
Example:
Input:
1 2
Output:
3
Reasoning:
- First, we initialize the sum to 0 and store the input values
a = 1andb = 2. - Then, we use bitwise operations to add the numbers:
a ^ bcalculates the sum without considering the carry (12=3), anda & bcalculates the carry (1 & 2 = 0). - Since there's no carry (0), the final result is just the sum from the XOR operation: 12=3.
- The final output is 3.
Constraints:
- -1000 <= a, b <= 1000
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.