13

Problem:

Consider the problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in binary form in an (n + 1)-element array C. State the problem formally and write pseudocode for adding the two integers.

Solution:

  1. C ← [1 ... n + 1] ▹ C is zero-filled.
  2. for i ← 1 to n
  3. do sum ← A[i] + B[i] + C[i]
  4. C[i] ← sum % 2
  5. C[i + 1] ← sum / 2 ▹ Integer division.
  6. output C

Question:

  1. I thought the C[i] is A[i]+B[i] why are you adding sum ← A[i] + B[i] + C[i] in step 3?
  2. why sum % 2 (why need to use modulo in step 4?)
  3. why sum / 2 (why need to use division in step 5?)

Could you please explain above solution with real example? Thanks.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
user_1357
  • 7,766
  • 13
  • 63
  • 106
  • consider how you add _by hand_ decimal numbers such as `179 + 256`. You work digit by digit, 'carrying' any results that are larger than the base into the 'cell' to the left... Try working a few examples of decimal additions by hand, then try binary additions. :) – sarnold Apr 10 '11 at 05:57

3 Answers3

20

C is both the solution and the carry. For a real example, let's add 11 + 3. I'll write in binary with decimal in parens)

A = 1011 (11) + B = 0011 (3) [C starts as 00000 (0)]
       ^               ^                      ^

The ^s mark the first position, and we go left, since we read left to right, but math goes right to left. Also, we divide integers, so 3/2 = 1, not 1.5. Now the steps.

1. sum = 1+1+0 = 10 (2), c[1] = 2 % 2 = 0, c[2] = 2/2 = 1
2. sum = 1+1+1 = 11 (3), c[2] = 3 % 2 = 1, c[3] = 3/2 = 1
3. sum = 0+0+1 = 01 (1), c[3] = 1 % 2 = 1, c[4] = 1/2 = 0
4. sum = 1+0+0 = 01 (1), c[4] = 1 % 2 = 1, c[5] = 1/2 = 0
^        ^ ^ ^                               ^
i        A B C, all at position i            note that we store the carry for the NEXT step

Result: C = 01110 (14)

Sajid
  • 4,381
  • 20
  • 14
5
  1. You add C[i] as well because C[i] may contain a carry bit from when you added A[i-1] + B[i-1] + C[i-1] in the previous iteration. For example if we do 1 + 1, our first iteration sum = 1 + 1 + 0 = 2, but since this is binary we have to carry the 1 and put it on C[1] and put the remainder (2 % 2 = 0) in C[0]. This gives us 10
  2. C[i] gets sum % 2 because the sum of A[i] + B[i] + C[i] could be more than 1, but 1 is the most that will fit in that digit. The rest of the sum (if there is any) will be put in the carry bit. And that brings us to...
  3. C[i+1] gets assigned sum / 2 because sum / 2 is the carry amount. It will be used in the next iteration when we do A[i] + B[i] + C[i] for i = i + 1.
David Brown
  • 13,336
  • 4
  • 38
  • 55
5

You can think of adding binary numbers the same way you add base 10 numbers: there is an "add" step and a "carry" step to perform at each digit.

So, let's take the math one bit at a time. Say we're adding:

101
+
011

For the first step, we start on the far-right. (In your example, this corresponds to i=1). We add (1+1)%2, which gives us 0. What's really going on here? 1+1 is 2, which in binary is a two-digit number ("10"). We can only write the lower-order digit ("0"), so expressing the sum "mod 2" is really just saying "don't worry about the carry-over sum for now." So we've got:

101
+
011
---
  0  (carrying a 1)

Now we implement the "carry a 1" by doing integer division ("sum / 2"), and temporarily storing it:

101
+
011
---
 10

Now we are ready to add the 2nd digits: (0+1)%2 -- but we must add in the carry-over 1 that we've been keeping track of, so we take (0+1+1)%2 yielding:

101
+
011
---
 00

Again we need to keep track of carry bit, giving us (0+1+1)=1:

101
+
011
---
100

Finally we add the 3rd bits: (1+0+1)%2 to give the answer:

 101
 +
 011
 ---
1000
Bosh
  • 8,138
  • 11
  • 51
  • 77