Add Two Integers Using Only Bitwise Operations
Last updated: November 29, 2026
Quick Overview
Implement addition of two 32-bit signed integers without using + or -, using only bitwise operators, and return the correct signed result.
Meta
November 29, 20260
0
0 solved
Implement addition of two 32-bit signed integers without using + or -, using only bitwise operators, and return the correct signed result.
A Meta phone-screen question that checks understanding of how hardware adders work. In Python the twist is unbounded integers, so the interviewer looks for explicit 32-bit masking and sign recovery.
What the Interviewer Expects
- Explain XOR as sum without carry and AND shifted as carry
- Loop until no carry remains
- Mask to 32 bits in Python to guarantee termination
- Convert the unsigned result back to signed
- Explain why the loop terminates in at most 32 rounds
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Implement subtraction the same way.
- Why does the Python loop run forever on negatives without a mask?
- How does a carry-lookahead adder speed this up in hardware?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: two integers in [-2^31, 2^31). Output: their sum, wrapped to 32-bit signed as hardware would. Forbidden: +, -, sum. Must work for negative operands....
High-Level Design
Binary addition splits into two parts: a ^ b adds each bit ignoring carries, and (a & b) << 1 is the carry into the next position. Replace a with the partial sum and b with the carry and repeat. E...