Signed Numbers, Complement Arithmetic and Overflow
In questa pagina 6
How can negative numbers be represented so that the same circuit performs sums and subtractions? With two's complement. The adder itself is in Binary Adders and SubtractorsHalf adder: S = X xor Y, C = XY. Full adder with carry in Z: S = X xor Y xor Z, C = XY + XZ + YZ = G + PZ with P = X xor Y, G = XY; two half adders and an OR. The n-bit ripple carry adder chains n full adders, so delay grows linearly with n. Adder-subtractor: XOR of each B bit with a control S, which is also the carry in, computes A+B or A+not B+1 = A-B. The 2-bit multiplier uses 4 ANDs and 2 half adders.Binary Adders and Subtractors →.
Unsigned subtraction
- If : no borrow out of the MSB and the result is correct.
- If : borrow out, and the result is , wrong. The correct absolute value is minus the result (its two's complement), with negative sign.
Example (): () gives () with borrow out. Absolute value , so the answer is . With : () (), no borrow.
Complements
Definition. For on bits: one's complement , i.e. every bit inverted; two's complement , i.e. one's complement plus 1.
(7 bits): one's complement , two's complement . Also ; . The complement of the complement returns ; the two's complement of is (carry discarded).
Subtraction with two's complement. : add to the two's complement of and remove .
- If the sum produces a carry out (the term), discarded: remains.
- If there is no carry out and the sum is , the two's complement of the difference; take the complement again for the magnitude and put a minus sign.
Signed numbers
Sign-magnitude. A sign bit ( positive, negative) is prefixed. With 4 bits the range is to but zero has two representations ( and ); operations treat sign and magnitude separately, so subtraction needs a sign correction.
Two's complement. Positives are as unsigned ( then the magnitude); a negative is the two's complement of , i.e. . MSB for positives, for negatives.
| bits | unsigned | sign-magnitude | two's complement |
|---|---|---|---|
| 0000 | 0 | 0 | |
| 0111 | 7 | 7 | 7 |
| 1000 | 8 | ||
| 1001 | 9 | ||
| 1010 | 10 | ||
| 1011 | 11 | ||
| 1100 | 12 | ||
| 1101 | 13 | ||
| 1110 | 14 | ||
| 1111 | 15 |
In two's complement there is one zero and the range is asymmetric: to (8 bits: to ; is not representable). The value is read giving the MSB the weight : ; 4 bits: .
8-bit examples: ; : sign-magnitude , two's complement (invert , add 1); ; . Sign extension: replicate the MSB ( is still ). Computers use two's complement because addition and subtraction share the same hardware.
Signed addition and subtraction
Sum: add the numbers including the sign bits and discard the carry out of the sign bit. 8-bit examples:
| operation | operands | result | value |
|---|---|---|---|
Subtraction. (one's complement of plus 1).
: , its two's complement is , and . At 4 bits, : , carry discarded. The adder-subtractor circuit that implements is in Binary Adders and SubtractorsHalf adder: S = X xor Y, C = XY. Full adder with carry in Z: S = X xor Y xor Z, C = XY + XZ + YZ = G + PZ with P = X xor Y, G = XY; two half adders and an OR. The n-bit ripple carry adder chains n full adders, so delay grows linearly with n. Adder-subtractor: XOR of each B bit with a control S, which is also the carry in, computes A+B or A+not B+1 = A-B. The 2-bit multiplier uses 4 ANDs and 2 half adders.Binary Adders and Subtractors →.
Overflow
Overflow occurs when the result does not fit in the available bits (e.g. on 4 bits). The system must detect it.
Unsigned. In an addition, overflow iff the carry out of the MSB is 1. In a subtraction there is no overflow; the carry out of the adder-subtractor is if (correct) and if (result to correct), i.e. an inverted borrow.
Signed (two's complement). Operands of different sign never overflow. Overflow occurs only if the two addends (for subtraction: and ) have the same sign and the result has the opposite sign. In terms of carries: overflow iff the carry into the sign bit differs from the carry out of it,
Why: two positives summing above send a carry into the sign bit (which becomes 1) but none out; two negatives below give carry out but none in. At 4 bits : , carry into sign , out , ( would be ).
8-bit examples (range ):
- : ; carries in, out, (read as ).
- : ; carries in, out, (read as ).
- : , carries , (at the limit, correct).
- : , carries , (correct).
Reference exercises (two's complement, MSB = sign):
- (6 bits, ): sum , carries : no overflow, .
- (7 bits, ): sum , carries in, out: overflow ().
- (): , , no overflow.
- (, 7 bits): two's complement of the subtrahend ; ; carries : no overflow, .
Common mistakes
- Using the carry out as the overflow flag for signed numbers: compare the carry in and out of the sign bit.
- Inverting the bits without adding 1.
- Assuming is representable.
- Extending a negative number with zeros instead of replicating the sign.
- Writing on 8 bits as (that is sign-magnitude; two's complement is ).
Versione ripasso
- Unsigned subtraction: : no borrow out, correct. : result is wrong; magnitude result, sign minus (, , so ).
- Complements: one's (invert bits); two's one's : .
- Signed numbers: sign-magnitude has two zeros, symmetric range. Two's complement: one zero, range , MSB weight : ; 4 bits . Sign extension replicates the MSB.
- Sum: add including sign, discard carry out of the sign bit: ; .
- Subtraction: , same adder: .
- Unsigned overflow: in an addition iff carry out .
- Signed overflow: ; operands of different sign never overflow; , ; , ; on 7 bits overflows.
- 4-bit two's complement: , , , , . 8-bit: , .
- Mistakes: carry out used as signed overflow; complement without ; written ; taken as representable; zero extension of negatives.