Add two numbers without using add operator (i.e. Plus Sign)
Add two number without using + operator.
Algorithm:
- Iterate till second operand is not zero (0)
- Compute sum_without_carry by xor operator
- Compute carry by and operator and one left shift
- Replace operand1 with sum_without_carry and operand2 with carry
Latest Source Code:
Github: AddWithoutAddOperator.java
Output:
3 + 4 = 7 0 + 4 = 4 2 + 4 = 6 3 + 7 = 10 (-3) + (-7) = -10 3 + (-7) = -4 (-3) + 7 = 4