Category Archives: Bit

Add two numbers without using add operator (i.e. Plus Sign)

Add two numbers without using add operator (i.e. Plus Sign)

Add two number without using + operator.
Algorithm:
  1. Iterate till second operand is not zero (0)
    1. Compute sum_without_carry by xor operator
    2. Compute carry by and operator and one left shift
    3. 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