About

2.3 Integer Arithmetic, Overflow & Multiplication

Theory: Integer Arithmetic, Overflow & Multiplication

Section 2.2 showed three ways to encode signed integers. Here we see how each one is added and subtracted, how the hardware detects that a result does not fit (overflow), how a single adder circuit does both addition and subtraction, and how a simple sequential multiplier builds a product out of additions and shifts (Null & Lobur, Ch. 2; adder/multiplier hardware as in the lecture).

Signed-Magnitude Addition & Subtraction

Signed magnitude works exactly like pencil-and-paper arithmetic with signs. To compute A+BA + B:

  1. Same signs: add the magnitudes, and give the result the common sign.
  2. Different signs: subtract the smaller magnitude from the larger, and give the result the sign of the larger magnitude.
  3. Subtraction A−BA - B is done as A+(−B)A + (-B): flip the sign bit of BB, then apply rules 1–2.
  4. Overflow: if adding magnitudes produces a carry out of the leftmost magnitude bit, the result is too big and is lost. (The sign bit does not take part in the addition, so the carry cannot spill into it.)

Mini-example (8 bits: sign + 7-bit magnitude)

−46+(−25)-46 + (-25): signs are equal (both negative), so add the magnitudes:

    0101110   (46)
  + 0011001   (25)
  ---------
    1000111   (71)   no carry out of the magnitude, so no overflow

Attach the sign 1: result 1 1000111=−711\,1000111 = -71 ✓.

Signed magnitude needs extra hardware to compare signs, compare magnitudes, and choose add or subtract. That complexity is why computers prefer complement systems.

One’s Complement Addition (End-Around Carry)

In a complement system, subtraction is turned into addition: A−B=A+(−B)A - B = A + (-B), where −B-B is the complement of BB. Then:

  1. Add the two nn-bit patterns as ordinary unsigned binary, including the sign bits.
  2. If there is a carry out of the MSB, do not throw it away: add it back into the least significant bit (the end-around carry).
  3. The result is in one’s complement form (if the sign bit is 1, flip the bits to read its magnitude).

Why the end-around carry? One’s complement represents −B-B as (2n−1)−B(2^n - 1) - B. Adding gives A+2n−1−BA + 2^n - 1 - B. A carry out means the sum reached 2n2^n; dropping it removes 2n2^n, leaving A−B−1A - B - 1, so we must add the 1 back.

Advantages: only an adder is needed. Disadvantages: two zeros, and the possible second addition (two-step operation) costs time.

Mini-example

23+(−9)23 + (-9) in 8 bits: 23=0001011123 = 00010111, −9=11110110-9 = 11110110.

  carry out 1 <-
       0001 0111   (23)
     + 1111 0110   (-9)
     -----------
       0000 1101   (carry out of MSB was 1)
     +         1   (end-around carry)
     -----------
       0000 1110   = 14

Two’s Complement Addition & Subtraction

With two’s complement, all we do is add:

  1. Add the two nn-bit patterns, including sign bits.
  2. Discard any carry out of the MSB.
  3. Subtraction: A−B=A+(two’s complement of B)=A+B′+1A - B = A + (\text{two's complement of } B) = A + B' + 1.

Why discarding works: −B-B is stored as 2n−B2^n - B, so A+(2n−B)=(A−B)+2nA + (2^n - B) = (A - B) + 2^n. The extra 2n2^n is exactly the carry out of bit n−1n-1, so dropping it leaves A−BA - B.

Mini-example

48+(−19)48 + (-19) in 8 bits: 48=0011000048 = 00110000, −19=11101101-19 = 11101101.

       0011 0000   (48)
     + 1110 1101   (-19)
     -----------
     1 0001 1101   -> discard the carry -> 0001 1101 = 29

Overflow

Overflow is the error that occurs when the true result is outside the representable range (for 4-bit two’s complement: −8-8 to +7+7). The bits that come out look like a valid number but are wrong.

Overflow rule (two’s complement): look at the most significant bit position. Overflow occurs if and only if the carry into the MSB ≠ the carry out of the MSB. In hardware: V=Cin,MSB⊕Cout,MSBV = C_{\text{in,MSB}} \oplus C_{\text{out,MSB}}.

Equivalent “sign rule”:

A carry out of the MSB is not the same as overflow in two’s complement. (−4)+(−1)=1100+1111=1 1011(-4) + (-1) = 1100 + 1111 = 1\,1011 produces a carry out but the answer 1011=−51011 = -5 is correct. Only the carry-in/carry-out mismatch signals overflow.

Mini-example (4 bits)

5+45 + 4: 0101+01000101 + 0100. At bit 2: 1+1=01 + 1 = 0 carry 1, so the carry into the MSB is 1. At the MSB: 0+0+1=10 + 0 + 1 = 1, carry out 0. Since 1≠01 \ne 0: overflow. The pattern 10011001 reads as −7-7, but 5+4=95 + 4 = 9 does not fit in −8-8 to +7+7.

Hardware for Addition and Subtraction

The lecture’s block diagram uses two registers, a complementer, a switch and an adder:

Diagram loads as you scroll.

Gate-level version. The complementer and switch are usually built from XOR gates with one control line MM:

Bi⊕M={BiM=0 (add)Bi′M=1 (subtract)B_i \oplus M = \begin{cases} B_i & M = 0 \ (\text{add}) \\ B_i' & M = 1 \ (\text{subtract}) \end{cases}

MM is also fed in as the carry-in C0C_0 of the ripple-carry adder, which supplies the “+1” of the two’s complement. The overflow flag is one more XOR on the last two carries: V=Cn−1⊕CnV = C_{n-1} \oplus C_n (carry into vs carry out of the MSB).

  M (0 = add, 1 = subtract) goes to every XOR and is also C0
              |
     B3,M      B2,M      B1,M      B0,M
      XOR       XOR       XOR       XOR
       |  A3     |  A2     |  A1     |  A0
       |  |      |  |      |  |      |  |
      [ FA3 ]<-C3-[ FA2 ]<-C2-[ FA1 ]<-C1-[ FA0 ]<- C0 = M
     |    |          |          |          |
    C4   S3         S2         S1         S0

  V (overflow) = C3 XOR C4

Mini-example

M=1M = 1, A=0110A = 0110 (6), B=0011B = 0011 (3). XOR outputs: B⊕1111=1100B \oplus 1111 = 1100. Adder: 0110+1100+1=1 00110110 + 1100 + 1 = 1\,0011 → S=0011=3S = 0011 = 3 ✓ (6−36 - 3). C3=1C_3 = 1, C4=1C_4 = 1, so V=0V = 0.

Multiplication: the Sequential (Shift-and-Add) Multiplier

On paper, binary multiplication is easy because each multiplier bit is 0 or 1: each partial product is either 0 or a shifted copy of the multiplicand.

        0011   (3, multiplicand)
      x 0101   (5, multiplier)
      ------
        0011   bit 0 = 1 -> multiplicand
       0000    bit 1 = 0 -> zero
      0011     bit 2 = 1 -> multiplicand shifted 2
     0000      bit 3 = 0 -> zero
     -------
     0001111   = 15

Shift-and-add algorithm (multiplicand shifts left):

  1. Product ←0\leftarrow 0.
  2. For each multiplier bit, starting from the LSB:
    • if the bit is 1, add the (shifted) multiplicand to the product;
    • shift the multiplicand one place left.
  3. After all nn bits, the product (up to 2n2n bits) is complete.

Hardware version (the lecture’s sequential multiplier). Instead of shifting the multiplicand left (which needs a 2n2n-bit adder), real hardware keeps the multiplicand fixed and shifts the partial product right. Registers:

RegisterSizeHolds
Mnn bitsmultiplicand
Qnn bitsmultiplier (its bits are consumed from Q0Q_0) and, at the end, the low half of the product
Ann bitsaccumulator, starts at 0; ends as the high half of the product
C1 bitcarry out of the adder
Countnumber of bits still to process, starts at nn

Diagram loads as you scroll.

Each cycle examines one multiplier bit (Q0Q_0), possibly adds, then shifts the whole C A QC\,A\,Q chain right: C→An−1C \to A_{n-1}, A0→Qn−1A_0 \to Q_{n-1}, and Q0Q_0 falls off (it has been used).

Mini-example

With 4-bit registers, 3×53 \times 5 starts with M=0011M = 0011, A=0000A = 0000, Q=0101Q = 0101. After 4 cycles A Q=0000 1111=15A\,Q = 0000\,1111 = 15. (The full 8-bit trace is Example 6.)


Worked Examples

Exercise 1

Using signed-magnitude arithmetic, compute:

(a) −46+(−25)-46 + (-25) (8 bits) (b) 46+(−25)46 + (-25) (8 bits) (c) 107+25107 + 25 (8 bits)

(d) With 4-bit signed magnitude (sign + 3-bit magnitude): 3+43 + 4, −4−1-4 - 1, 5+45 + 4, −7−6-7 - 6.

Show solution ↓
Solution

8-bit signed magnitude = 1 sign bit + 7-bit magnitude. 46=0101110246 = 0101110_2, 25=0011001225 = 0011001_2, 107=11010112107 = 1101011_2.

(a) Same signs (both −): add magnitudes.

    0101110   (46)
  + 0011001   (25)
  ---------
    1000111   (71)

No carry out of the 7-bit magnitude. Sign = 1: 1 1000111=−711\,1000111 = -71 ✓.

(b) Different signs: subtract the smaller magnitude (25) from the larger (46); the result takes the sign of the larger (+).

    0101110   (46)
  - 0011001   (25)
  ---------
    0010101   (21)

Result 0 0010101=+210\,0010101 = +21 ✓.

(c) Same signs (both +): add magnitudes.

    1101011   (107)
  + 0011001   (25)
  ---------
  1 0000100

There is a carry out of the magnitude: the true sum 132132 exceeds the maximum 127127. Overflow; the stored magnitude 0000100=40000100 = 4 is meaningless.

(d) 4-bit signed magnitude, magnitude range 00 to 77:

OperationMagnitudesSumResult
3+43 + 4011+100011 + 1001111110 111=+70\,111 = +7 ✓
−4−1=(−4)+(−1)-4 - 1 = (-4) + (-1)100+001100 + 0011011011 101=−51\,101 = -5 ✓
5+45 + 4101+100101 + 1001 0011\,001 (carry out)overflow (9 does not fit)
−7−6=(−7)+(−6)-7 - 6 = (-7) + (-6)111+110111 + 1101 1011\,101 (carry out)overflow (−13-13 does not fit)
Exercise 2

Using 8-bit one’s complement arithmetic, compute (a) 23+(−9)23 + (-9) and (b) 9+(−23)9 + (-23).

Show solution ↓
Solution

23=0001 011123 = 0001\,0111, so −23=1110 1000-23 = 1110\,1000 (flip). 9=0000 10019 = 0000\,1001, so −9=1111 0110-9 = 1111\,0110 (flip).

(a)

  carry out 1 <-
            0001 0111   (23)
          + 1111 0110   (-9)
          -----------
            0000 1101
          +         1   (end-around carry)
          -----------
            0000 1110   = +14

Column by column from the right: 1+0=11+0 = 1; 1+1=01+1 = 0 c1; 1+1+1=11+1+1 = 1 c1; 0+0+1=10+0+1 = 1; 1+1=01+1 = 0 c1; 0+1+1=00+1+1 = 0 c1; 0+1+1=00+1+1 = 0 c1; 0+1+1=00+1+1 = 0 c1 → carry out 1. Adding it back to the LSB gives 0000 1110=140000\,1110 = 14 ✓.

(b)

  carry out 0 <-
            0000 1001   (9)
          + 1110 1000   (-23)
          -----------
            1111 0001

The carry out of the MSB is 0, so we are done. The sign bit is 1 (negative); flip to read the magnitude: 0000 1110=140000\,1110 = 14. Result −14-14 ✓.

Exercise 3

Using 8-bit two’s complement arithmetic, compute (a) 48+(−19)48 + (-19) and (b) 19−4819 - 48.

Show solution ↓
Solution

48=0011 000048 = 0011\,0000; 19=0001 001119 = 0001\,0011. −19-19: flip 1110 11001110\,1100, +1+1 → 1110 11011110\,1101. −48-48: flip 1100 11111100\,1111, +1+1 → 1101 00001101\,0000.

(a)

            0011 0000   (48)
          + 1110 1101   (-19)
          -----------
          1 0001 1101

Discard the carry out of the MSB: 0001 1101=16+8+4+1=290001\,1101 = 16 + 8 + 4 + 1 = 29 ✓. (Carry into MSB = 1 and carry out = 1: equal, so no overflow.)

(b) 19−48=19+(−48)19 - 48 = 19 + (-48):

            0001 0011   (19)
          + 1101 0000   (-48)
          -----------
            1110 0011

No carry out. Sign bit 1, so negative; magnitude: flip 0001 11000001\,1100, +1+1 → 0001 1101=290001\,1101 = 29. Result −29-29 ✓. (Carry in = carry out = 0: no overflow.)

Exercise 4

(a) Perform each 4-bit two’s complement addition, record the carry into and out of the MSB, and decide whether overflow occurred: (i) (−7)+(+5)(-7) + (+5) (ii) (−4)+(+4)(-4) + (+4) (iii) (+3)+(+4)(+3) + (+4) (iv) (−4)+(−1)(-4) + (-1) (v) (+5)+(+4)(+5) + (+4) (vi) (−7)+(−6)(-7) + (-6).

(b) Repeat for 8-bit 100+50100 + 50 and (−100)+(−50)(-100) + (-50).

Show solution ↓
Solution

(a) 4-bit range is −8-8 to +7+7. Patterns: −7=1001-7 = 1001, 5=01015 = 0101, −4=1100-4 = 1100, 4=01004 = 0100, 3=00113 = 0011, −1=1111-1 = 1111, −6=1010-6 = 1010.

CaseAdditionSum (4 bits)Carry into MSBCarry out of MSBOverflow?Meaning
(i) −7+5-7 + 51001+01011001 + 01011110111000no−2-2 ✓
(ii) −4+4-4 + 41100+01001100 + 0100(1) 0000(1)\,000011no00 ✓
(iii) 3+43 + 40011+01000011 + 01000111011100no77 ✓
(iv) −4+(−1)-4 + (-1)1100+11111100 + 1111(1) 1011(1)\,101111no−5-5 ✓
(v) 5+45 + 40101+01000101 + 01001001100110yestrue 9 does not fit
(vi) −7+(−6)-7 + (-6)1001+10101001 + 1010(1) 0011(1)\,001101yestrue −13-13 does not fit

Detail for (v): bit 0: 1+0=11+0 = 1; bit 1: 0+0=00+0 = 0; bit 2: 1+1=01+1 = 0 carry 1; bit 3 (MSB): 0+0+1=10+0+1 = 1, carry out 0. Carry in 1 ≠ carry out 0 → overflow. Two positives gave a “negative” result.

Detail for (vi): bit 0: 1+0=11+0 = 1; bit 1: 0+1=10+1 = 1; bit 2: 0+0=00+0 = 0, carry 0; MSB: 1+1+0=01+1+0 = 0, carry out 1. Carry in 0 ≠ carry out 1 → overflow. Two negatives gave a “positive” result (0011=+30011 = +3).

Note (ii) and (iv) have a carry out but no overflow, because carry in equals carry out.

(b) 8-bit range is −128-128 to +127+127.

100+50100 + 50: 0110 0100+0011 0010=1001 01100110\,0100 + 0011\,0010 = 1001\,0110. Carry into the MSB is 1, carry out is 0 → overflow. The pattern reads −106-106, but the true sum 150 exceeds 127.

(−100)+(−50)(-100) + (-50): −100=1001 1100-100 = 1001\,1100, −50=1100 1110-50 = 1100\,1110. Sum =1 0110 1010= 1\,0110\,1010. Carry into the MSB is 0, carry out is 1 → overflow. The pattern 0110 10100110\,1010 reads +106+106, but the true sum −150-150 is below −128-128.

Exercise 5

A 4-bit two’s complement adder/subtractor uses XOR gates on the BB inputs with control line MM (also fed in as carry-in C0C_0), and sets V=C3⊕C4V = C_3 \oplus C_4. Trace the circuit for (a) 6−36 - 3, (b) 5−(−4)5 - (-4), (c) (−3)+4(-3) + 4. Give the XOR outputs, all carries, the sum and VV.

Show solution ↓
Solution

CiC_i is the carry into bit ii; C4C_4 is the carry out of the MSB, C3C_3 the carry into it.

(a) 6−36 - 3: M=1M = 1, A=0110A = 0110, B=0011B = 0011, XOR outputs B⊕1111=1100B \oplus 1111 = 1100, C0=1C_0 = 1.

BitAiA_iBi⊕MB_i \oplus MCiC_iSiS_iCi+1C_{i+1}
000110
110010
211001
301101

S=0011=3S = 0011 = 3 ✓. C3=1C_3 = 1, C4=1C_4 = 1, so V=1⊕1=0V = 1 \oplus 1 = 0 (no overflow).

(b) 5−(−4)5 - (-4): M=1M = 1, A=0101A = 0101, B=1100B = 1100 (−4-4), XOR outputs 00110011, C0=1C_0 = 1.

BitAiA_iBi⊕MB_i \oplus MCiC_iSiS_iCi+1C_{i+1}
011111
101101
210101
300110

S=1001S = 1001. C3=1C_3 = 1, C4=0C_4 = 0, so V=1⊕0=1V = 1 \oplus 0 = 1: overflow. Indeed 5−(−4)=9>75 - (-4) = 9 \gt 7; the pattern 10011001 (−7-7) is wrong.

(c) (−3)+4(-3) + 4: M=0M = 0, A=1101A = 1101, B=0100B = 0100, XOR outputs 01000100 (unchanged), C0=0C_0 = 0.

BitAiA_iBi⊕MB_i \oplus MCiC_iSiS_iCi+1C_{i+1}
010010
100000
211001
310101

S=0001=1S = 0001 = 1 ✓. C3=1C_3 = 1, C4=1C_4 = 1, V=0V = 0. The carry out is simply discarded.

Exercise 6

Use the 8-bit sequential multiplier to compute 3×53 \times 5, with multiplicand M=00000011M = 00000011 (3) and multiplier Q=00000101Q = 00000101 (5). First show the shift-multiplicand-left view, then trace the registers CC, AA, QQ cycle by cycle.

Show solution ↓
Solution

View 1: shift-and-add, multiplicand moving left. Process multiplier bits from the LSB:

StepMultiplier bitActionProductMultiplicand after shift
start0000 00000000 0011
1q0=1q_0 = 1add 0000 00110000 00110000 0110
2q1=0q_1 = 0no add0000 00110000 1100
3q2=1q_2 = 1add 0000 11000000 11110001 1000
4–8q3q_3 to q7=0q_7 = 0no add0000 1111(keeps shifting)

Product =0000 1111=15= 0000\,1111 = 15 ✓.

View 2: the hardware registers (product shifts right). Initial: C=0C = 0, A=00000000A = 00000000, Q=00000101Q = 00000101, M=00000011M = 00000011, Count =8= 8. Each cycle: if Q0=1Q_0 = 1, C,A←A+MC, A \leftarrow A + M; then shift C A QC\,A\,Q right.

CycleQ0Q_0OperationCAQ
0initial values00000 00000000 0101
11add: A←A+MA \leftarrow A + M00000 00110000 0101
1shift right00000 00011000 0010
20shift right00000 00001100 0001
31add: A←A+MA \leftarrow A + M00000 00111100 0001
3shift right00000 00011110 0000
40shift right00000 00001111 0000
50shift right00000 00000111 1000
60shift right00000 00000011 1100
70shift right00000 00000001 1110
80shift right00000 00000000 1111

Count reaches 0 after cycle 8. The 16-bit product is A Q=0000 0000 0000 11112=15A\,Q = 0000\,0000\,0000\,1111_2 = 15 ✓.

Watch the shift in cycle 1: A=0000 0011A = 0000\,0011‘s LSB (1) moves into the MSB of QQ, giving Q=1000 0010Q = 1000\,0010; the old Q0Q_0 (the used multiplier bit) drops off. After 8 cycles every multiplier bit has been shifted out and replaced by product bits.

Exercise 7

Trace a 4-bit sequential multiplier for 11×1311 \times 13 (multiplicand M=1011M = 1011, multiplier Q=1101Q = 1101). This time the CC bit matters.

Show solution ↓
Solution

Initial: C=0C = 0, A=0000A = 0000, Q=1101Q = 1101, M=1011M = 1011, Count =4= 4.

CycleQ0Q_0OperationCAQ
0initial values000001101
11add: 0000+10110000 + 1011010111101
1shift right001011110
20shift right000101111
31add: 0010+10110010 + 1011011011111
3shift right001101111
41add: 0110+1011=1 00010110 + 1011 = 1\,0001100011111
4shift right010001111

In cycle 4 the 4-bit addition overflows into CC (6+11=17=1 000126 + 11 = 17 = 1\,0001_2). The right shift moves CC into A3A_3, so the carry is not lost.

Product: A Q=1000 11112=128+15=143A\,Q = 1000\,1111_2 = 128 + 15 = 143. Check: 11×13=14311 \times 13 = 143 ✓.