A B without arithmetic operators, Python vs C
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Adding two integers without using + is a classic bit-manipulation exercise. The core idea is the same in Python and C or C++, but the language-level integer model changes how you handle negative numbers and overflow.
The Bitwise Idea
Binary addition can be split into two operations:
- XOR gives the sum bits without carry
- AND plus a left shift gives the carry bits
Then you repeat until there is no carry left.
Mathematically, the pattern is:
- '
sum = a ^ b' - '
carry = (a & b) << 1' - repeat with
a = sumandb = carry
That is the whole trick.
The C or C++ Version
In a fixed-width integer model, the loop is straightforward. Use unsigned integers for the bitwise work so left shifts and overflow behavior stay well-defined.
This works because C and C++ integer types have a fixed bit width. The carries eventually run out within that width.
Why Python Is Different
Python integers are not fixed-width machine integers. They grow to arbitrary precision, which is normally a feature, but it changes the bitwise-addition trick.
If you write the same loop naively in Python and involve negative numbers, the carry may not vanish the way it does in a fixed-width two’s-complement model.
That is why Python solutions usually simulate a fixed-width register with a mask.
A Python Version with 32-Bit Masking
Here is a common 32-bit signed implementation:
The mask forces the computation to behave like 32-bit arithmetic. The final line converts the masked result back into a Python signed integer.
Why the Mask Is Needed
In a fixed-width system, negative values are represented with a specific number of bits. In Python, integers conceptually have unlimited sign extension, so the carry can keep propagating if you do not clamp the width.
That means the algorithmic idea is identical across languages, but Python needs extra machinery to imitate the finite-width arithmetic model that C and C++ already use.
What the Exercise Really Teaches
This problem is more about understanding binary addition than about writing production code. In normal application code, just use +.
The exercise is still useful because it teaches:
- how carry propagation works
- why XOR behaves like addition without carry
- how language-level integer semantics affect bitwise code
That last point is the most important Python-versus-C difference.
Step-by-Step Example
Take 5 and 3.
Binary forms:
- '
5is0101' - '
3is0011'
First pass:
- XOR gives
0110 - AND gives
0001 - shift carry left to get
0010
Second pass with 0110 and 0010:
- XOR gives
0100 - AND gives
0010 - shift carry left to get
0100
Third pass with 0100 and 0100:
- XOR gives
0000 - AND gives
0100 - shift carry left to get
1000
Final pass with 0000 and 1000:
- XOR gives
1000 - carry becomes
0000
Result: 8.
Common Pitfalls
The biggest mistake in Python is forgetting the fixed-width mask. The loop may not behave correctly for negative values without it.
Another mistake in C or C++ is doing the bitwise work on signed integers and assuming overflow or left-shift behavior is harmless. Using unsigned types for the internal loop is safer.
Developers also sometimes think this technique is more efficient than + in normal code. It is not a practical replacement; it is an educational exercise.
Finally, be explicit about the chosen width in Python. A 32-bit simulation and a 64-bit simulation are not the same when large values are involved.
Summary
- Bitwise addition uses XOR for partial sums and AND-plus-shift for carries.
- The core algorithm is the same in Python and C or C++.
- Python needs masking because its integers are arbitrary precision.
- C or C++ code should use unsigned types internally to avoid undefined or surprising signed-bit behavior.
- This is mainly a binary arithmetic exercise, not a production replacement for
+.

