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+B:
- Same signs: add the magnitudes, and give the result the common sign.
- Different signs: subtract the smaller magnitude from the larger, and give the result the sign of the larger magnitude.
- Subtraction A−B is done as A+(−B): flip the sign bit of B, then apply rules 1–2.
- 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): 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 11000111=−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), where −B is the complement of B. Then:
- Add the two n-bit patterns as ordinary unsigned binary, including the sign bits.
- 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).
- 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 as (2n−1)−B. Adding gives A+2n−1−B. A carry out means the sum reached 2n; dropping it removes 2n, leaving A−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) in 8 bits: 23=00010111, −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:
- Add the two n-bit patterns, including sign bits.
- Discard any carry out of the MSB.
- Subtraction: A−B=A+(two’s complement of B)=A+B′+1.
Why discarding works: −B is stored as 2n−B, so A+(2n−B)=(A−B)+2n. The extra 2n is exactly the carry out of bit n−1, so dropping it leaves A−B.
Mini-example
48+(−19) in 8 bits: 48=00110000, −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 to +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,MSB.
Equivalent “sign rule”:
- Adding two numbers of different signs never overflows (the result lies between them).
- Adding two numbers of the same sign overflows exactly when the result has the opposite sign.
A carry out of the MSB is not the same as overflow in two’s complement. (−4)+(−1)=1100+1111=11011 produces a carry out but the answer 1011=−5 is correct. Only the carry-in/carry-out mismatch signals overflow.
Mini-example (4 bits)
5+4: 0101+0100. At bit 2: 1+1=0 carry 1, so the carry into the MSB is 1. At the MSB: 0+0+1=1, carry out 0. Since 1=0: overflow. The pattern 1001 reads as −7, but 5+4=9 does not fit in −8 to +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.
- For addition, the switch passes B unchanged to the adder.
- For subtraction, the switch passes the complement of B (and the adder adds 1 more), so the adder computes A+B′+1=A−B.
- The result is written back into the A register; the OF flag is set when overflow occurs (1 = overflow, 0 = no overflow).
Gate-level version. The complementer and switch are usually built from XOR gates with one control line M:
Bi⊕M={BiBi′M=0 (add)M=1 (subtract)
M is also fed in as the carry-in C0 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⊕Cn (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=1, A=0110 (6), B=0011 (3). XOR outputs: B⊕1111=1100. Adder: 0110+1100+1=10011 → S=0011=3 ✓ (6−3). C3=1, C4=1, so V=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):
- Product ←0.
- 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.
- After all n bits, the product (up to 2n bits) is complete.
Hardware version (the lecture’s sequential multiplier). Instead of shifting the multiplicand left (which needs a 2n-bit adder), real hardware keeps the multiplicand fixed and shifts the partial product right. Registers:
| Register | Size | Holds |
|---|
| M | n bits | multiplicand |
| Q | n bits | multiplier (its bits are consumed from Q0) and, at the end, the low half of the product |
| A | n bits | accumulator, starts at 0; ends as the high half of the product |
| C | 1 bit | carry out of the adder |
| Count | | number of bits still to process, starts at n |
Diagram loads as you scroll.
Each cycle examines one multiplier bit (Q0), possibly adds, then shifts the whole CAQ chain right: C→An−1, A0→Qn−1, and Q0 falls off (it has been used).
Mini-example
With 4-bit registers, 3×5 starts with M=0011, A=0000, Q=0101. After 4 cycles AQ=00001111=15. (The full 8-bit trace is Example 6.)
Worked Examples
Exercise 1
Using signed-magnitude arithmetic, compute:
(a) −46+(−25) (8 bits) (b) 46+(−25) (8 bits) (c) 107+25 (8 bits)
(d) With 4-bit signed magnitude (sign + 3-bit magnitude): 3+4, −4−1, 5+4, −7−6.
Show solution ↓Hide solution ↑
Solution
8-bit signed magnitude = 1 sign bit + 7-bit magnitude. 46=01011102, 25=00110012, 107=11010112.
(a) Same signs (both −): add magnitudes.
0101110 (46)
+ 0011001 (25)
---------
1000111 (71)
No carry out of the 7-bit magnitude. Sign = 1: 11000111=−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 00010101=+21 ✓.
(c) Same signs (both +): add magnitudes.
1101011 (107)
+ 0011001 (25)
---------
1 0000100
There is a carry out of the magnitude: the true sum 132 exceeds the maximum 127. Overflow; the stored magnitude 0000100=4 is meaningless.
(d) 4-bit signed magnitude, magnitude range 0 to 7:
| Operation | Magnitudes | Sum | Result |
|---|
| 3+4 | 011+100 | 111 | 0111=+7 ✓ |
| −4−1=(−4)+(−1) | 100+001 | 101 | 1101=−5 ✓ |
| 5+4 | 101+100 | 1001 (carry out) | overflow (9 does not fit) |
| −7−6=(−7)+(−6) | 111+110 | 1101 (carry out) | overflow (−13 does not fit) |
Exercise 2
Using 8-bit one’s complement arithmetic, compute (a) 23+(−9) and (b) 9+(−23).
Show solution ↓Hide solution ↑
Solution
23=00010111, so −23=11101000 (flip). 9=00001001, so −9=11110110 (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=1; 1+1=0 c1; 1+1+1=1 c1; 0+0+1=1; 1+1=0 c1; 0+1+1=0 c1; 0+1+1=0 c1; 0+1+1=0 c1 → carry out 1. Adding it back to the LSB gives 00001110=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: 00001110=14. Result −14 ✓.
Exercise 3
Using 8-bit two’s complement arithmetic, compute (a) 48+(−19) and (b) 19−48.
Show solution ↓Hide solution ↑
Solution
48=00110000; 19=00010011.
−19: flip 11101100, +1 → 11101101.
−48: flip 11001111, +1 → 11010000.
(a)
0011 0000 (48)
+ 1110 1101 (-19)
-----------
1 0001 1101
Discard the carry out of the MSB: 00011101=16+8+4+1=29 ✓. (Carry into MSB = 1 and carry out = 1: equal, so no overflow.)
(b) 19−48=19+(−48):
0001 0011 (19)
+ 1101 0000 (-48)
-----------
1110 0011
No carry out. Sign bit 1, so negative; magnitude: flip 00011100, +1 → 00011101=29. Result −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) (ii) (−4)+(+4) (iii) (+3)+(+4) (iv) (−4)+(−1) (v) (+5)+(+4) (vi) (−7)+(−6).
(b) Repeat for 8-bit 100+50 and (−100)+(−50).
Show solution ↓Hide solution ↑
Solution
(a) 4-bit range is −8 to +7. Patterns: −7=1001, 5=0101, −4=1100, 4=0100, 3=0011, −1=1111, −6=1010.
| Case | Addition | Sum (4 bits) | Carry into MSB | Carry out of MSB | Overflow? | Meaning |
|---|
| (i) −7+5 | 1001+0101 | 1110 | 0 | 0 | no | −2 ✓ |
| (ii) −4+4 | 1100+0100 | (1)0000 | 1 | 1 | no | 0 ✓ |
| (iii) 3+4 | 0011+0100 | 0111 | 0 | 0 | no | 7 ✓ |
| (iv) −4+(−1) | 1100+1111 | (1)1011 | 1 | 1 | no | −5 ✓ |
| (v) 5+4 | 0101+0100 | 1001 | 1 | 0 | yes | true 9 does not fit |
| (vi) −7+(−6) | 1001+1010 | (1)0011 | 0 | 1 | yes | true −13 does not fit |
Detail for (v): bit 0: 1+0=1; bit 1: 0+0=0; bit 2: 1+1=0 carry 1; bit 3 (MSB): 0+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=1; bit 1: 0+1=1; bit 2: 0+0=0, carry 0; MSB: 1+1+0=0, carry out 1. Carry in 0 ≠ carry out 1 → overflow. Two negatives gave a “positive” result (0011=+3).
Note (ii) and (iv) have a carry out but no overflow, because carry in equals carry out.
(b) 8-bit range is −128 to +127.
100+50: 01100100+00110010=10010110. Carry into the MSB is 1, carry out is 0 → overflow. The pattern reads −106, but the true sum 150 exceeds 127.
(−100)+(−50): −100=10011100, −50=11001110. Sum =101101010. Carry into the MSB is 0, carry out is 1 → overflow. The pattern 01101010 reads +106, but the true sum −150 is below −128.
Exercise 5
A 4-bit two’s complement adder/subtractor uses XOR gates on the B inputs with control line M (also fed in as carry-in C0), and sets V=C3⊕C4. Trace the circuit for (a) 6−3, (b) 5−(−4), (c) (−3)+4. Give the XOR outputs, all carries, the sum and V.
Show solution ↓Hide solution ↑
Solution
Ci is the carry into bit i; C4 is the carry out of the MSB, C3 the carry into it.
(a) 6−3: M=1, A=0110, B=0011, XOR outputs B⊕1111=1100, C0=1.
| Bit | Ai | Bi⊕M | Ci | Si | Ci+1 |
|---|
| 0 | 0 | 0 | 1 | 1 | 0 |
| 1 | 1 | 0 | 0 | 1 | 0 |
| 2 | 1 | 1 | 0 | 0 | 1 |
| 3 | 0 | 1 | 1 | 0 | 1 |
S=0011=3 ✓. C3=1, C4=1, so V=1⊕1=0 (no overflow).
(b) 5−(−4): M=1, A=0101, B=1100 (−4), XOR outputs 0011, C0=1.
| Bit | Ai | Bi⊕M | Ci | Si | Ci+1 |
|---|
| 0 | 1 | 1 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 | 0 | 1 |
| 2 | 1 | 0 | 1 | 0 | 1 |
| 3 | 0 | 0 | 1 | 1 | 0 |
S=1001. C3=1, C4=0, so V=1⊕0=1: overflow. Indeed 5−(−4)=9>7; the pattern 1001 (−7) is wrong.
(c) (−3)+4: M=0, A=1101, B=0100, XOR outputs 0100 (unchanged), C0=0.
| Bit | Ai | Bi⊕M | Ci | Si | Ci+1 |
|---|
| 0 | 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 0 | 0 | 0 | 0 |
| 2 | 1 | 1 | 0 | 0 | 1 |
| 3 | 1 | 0 | 1 | 0 | 1 |
S=0001=1 ✓. C3=1, C4=1, V=0. The carry out is simply discarded.
Exercise 6
Use the 8-bit sequential multiplier to compute 3×5, with multiplicand M=00000011 (3) and multiplier Q=00000101 (5). First show the shift-multiplicand-left view, then trace the registers C, A, Q cycle by cycle.
Show solution ↓Hide solution ↑
Solution
View 1: shift-and-add, multiplicand moving left. Process multiplier bits from the LSB:
| Step | Multiplier bit | Action | Product | Multiplicand after shift |
|---|
| start | | | 0000 0000 | 0000 0011 |
| 1 | q0=1 | add 0000 0011 | 0000 0011 | 0000 0110 |
| 2 | q1=0 | no add | 0000 0011 | 0000 1100 |
| 3 | q2=1 | add 0000 1100 | 0000 1111 | 0001 1000 |
| 4–8 | q3 to q7=0 | no add | 0000 1111 | (keeps shifting) |
Product =00001111=15 ✓.
View 2: the hardware registers (product shifts right). Initial: C=0, A=00000000, Q=00000101, M=00000011, Count =8. Each cycle: if Q0=1, C,A←A+M; then shift CAQ right.
| Cycle | Q0 | Operation | C | A | Q |
|---|
| 0 | | initial values | 0 | 0000 0000 | 0000 0101 |
| 1 | 1 | add: A←A+M | 0 | 0000 0011 | 0000 0101 |
| 1 | | shift right | 0 | 0000 0001 | 1000 0010 |
| 2 | 0 | shift right | 0 | 0000 0000 | 1100 0001 |
| 3 | 1 | add: A←A+M | 0 | 0000 0011 | 1100 0001 |
| 3 | | shift right | 0 | 0000 0001 | 1110 0000 |
| 4 | 0 | shift right | 0 | 0000 0000 | 1111 0000 |
| 5 | 0 | shift right | 0 | 0000 0000 | 0111 1000 |
| 6 | 0 | shift right | 0 | 0000 0000 | 0011 1100 |
| 7 | 0 | shift right | 0 | 0000 0000 | 0001 1110 |
| 8 | 0 | shift right | 0 | 0000 0000 | 0000 1111 |
Count reaches 0 after cycle 8. The 16-bit product is AQ=00000000000011112=15 ✓.
Watch the shift in cycle 1: A=00000011‘s LSB (1) moves into the MSB of Q, giving Q=10000010; the old Q0 (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×13 (multiplicand M=1011, multiplier Q=1101). This time the C bit matters.
Show solution ↓Hide solution ↑
Solution
Initial: C=0, A=0000, Q=1101, M=1011, Count =4.
| Cycle | Q0 | Operation | C | A | Q |
|---|
| 0 | | initial values | 0 | 0000 | 1101 |
| 1 | 1 | add: 0000+1011 | 0 | 1011 | 1101 |
| 1 | | shift right | 0 | 0101 | 1110 |
| 2 | 0 | shift right | 0 | 0010 | 1111 |
| 3 | 1 | add: 0010+1011 | 0 | 1101 | 1111 |
| 3 | | shift right | 0 | 0110 | 1111 |
| 4 | 1 | add: 0110+1011=10001 | 1 | 0001 | 1111 |
| 4 | | shift right | 0 | 1000 | 1111 |
In cycle 4 the 4-bit addition overflows into C (6+11=17=100012). The right shift moves C into A3, so the carry is not lost.
Product: AQ=100011112=128+15=143. Check: 11×13=143 ✓.