Sum of Two Integers

medium

Add two integers without using + or - operators

Sum Without + or -

Key Insight

XOR gives sum without carry. AND << 1 gives carry. Repeat until no carry.

Step 1Problem
5
5
=
0
0
0
0
0
1
0
1
3
3
=
0
0
0
0
0
0
1
1
76543210
0101 + 0011

Add 5 + 3 without using + or -.

1 / 5

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Sum Without + or -

XOR gives sum without carry. AND << 1 gives carry. Repeat until no carry.

  1. Problem

    Add 5 + 3 without using + or -.

  2. XOR (Sum without carry)

    5 ^ 3 = 6. This is sum ignoring carries.

  3. AND << 1 (Carry)

    (5 & 3) << 1 = 2. These are the carry bits.

  4. Repeat

    6 ^ 2 = 4, (6 & 2) << 1 = 4

  5. Result

    When carry = 0, answer is 8!