Command Palette
Search for a command to run...
Problem 34.8 · Bit ManipulationMedium
What it teaches: Addition as XOR (sum without carry) plus AND-shift (the carries), repeated.
Practise it on judges as “Sum of Two Integers”.
The problem
Return a + b without using + or −.
Example 1
Input: a = 2, b = 3
Output: 5
Constraints
Pattern clues in the wording
- → Arithmetic without arithmetic operators
These clues point to Bit Manipulation: Use XOR, AND, OR and shifts to test, set and cancel bits, often in O(1) space.
Stuck? Take one hint at a time
Solution.java · starterclass Solution {
public int getSum(int a, int b) {
return 0;
}
}
Write your solution locally or in your editor for now. The in-browser runner (Java first, then Python, C++ and more) will run these tests right here.
Test cases
| # | Input | Expected |
|---|
| 1 | a = 1 b = 2 | 3 |
| 2 | a = 2 b = 3 | 5 |
+ 2 hidden tests the code runner will check