Sum without plus (CodeWars problem)
Today, while spending some time on Codewars, I came across a very interesting problem. The title was: “Sum of Two Integers.” At first, it…
Sum without plus (CodeWars problem)
Today, while spending some time on Codewars, I came across a very interesting problem. The title was: “Sum of Two Integers.” At first, it seemed like a simple question but something about it made me curious, so I clicked on it The challenge was: How can we add two integers x and y without using the + or - operators?
For example, calling add(1, 3) should return 4. I stopped and thought: “How is that even possible? Adding numbers… without adding?” It almost felt like a joke. But I’m not the kind of person who gives up easily, so I decided to dig deeper.
Suddenly, I remembered something from my university days “Logic Design” Yes! In that course, we learned how to perform arithmetic using logic gates. Words like XOR, AND, and SHIFT started popping into my mind. I realized that addition is not just about using the + symbol. If we want, we can represent it using pure logic operations, just like in digital circuits.
I grabbed a pen and paper. To add two numbers at the bit level, we need to understand how sum and carry work. This is exactly what a full adder circuit does. Then I had another realization: This is how computers actually do it too! Inside the CPU, there’s something called an ALU (Arithmetic Logic Unit). It doesn’t understand the + symbol like we do. It only performs bitwise operations like XOR and AND.
The logic is this:
XORgives the sum without carry
AND + shift left (<< 1)gives the carry bits
If we repeat this process until the carry becomes zero, we’ll get the final result!
public class Solution {
public static int add(int x, int y) {
while (y != 0) {
int carry = (x & y) << 1;
x = x ^ y;
y = carry;
}
return x;
}
}
I found this solution so elegant, so mathematical, and a little bit hacky in a good way. It reminded me that programming is not just about using built-in operators or functions. It’s about solving problems from different angles. This challenge made me realize something important:
Forget what you know. Find a new path.
It wasn’t just about adding two numbers anymore it became a new way of thinking. By applying knowledge from logic circuits and bit manipulation, I solved a basic math operation in a completely different way. It was a great reminder of what being a developer is really about.
메타데이터
- post_id
- 6d19f08c2c6d
- slug
- sum-without-plus-codewars-problem-6d19f08c2c6d
- url
- https://medium.com/@efecanerdem0907/sum-without-plus-codewars-problem-6d19f08c2c6d
- canonical_url
- https://medium.com/@efecanerdem0907/sum-without-plus-codewars-problem-6d19f08c2c6d
- author_url
- https://medium.com/@efecanerdem0907
- status
- ok
- fetched_at
- 2026-08-07 15:40:12