Salta al contenuto
Note per Studenti Signed Numbers, Complement Arithmetic and Overflow Report

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

Subtract NN from MM (nn bits) bit by bit with borrows (Number Systems and Base ConversionsA number in base r is the sum of a_i r^i with digits from 0 to r-1. Conversions: base r to decimal by weighted sum; decimal to base r by successive divisions (integer part, remainders read bottom-up) and successive multiplications (fractional part, integer parts read top-down); binary to octal or hexadecimal by groups of 3 or 4 bits starting from the radix point. Binary sum, difference and product follow the decimal rules with borrows and shifted partial products.Number Systems and Base Conversions →).

  • If M≥NM\ge N: no borrow out of the MSB and the result is correct.
  • If M<NM<N: borrow out, and the result is M−N+2nM-N+2^n, wrong. The correct absolute value is 2n2^n minus the result (its two's complement), with negative sign.

Example (n=8n=8): 01100100−1001011001100100-10010110 (100−150100-150) gives 1100111011001110 (206=100−150+256206=100-150+256) with borrow out. Absolute value 256−206=50=00110010256-206=50=00110010, so the answer is −50-50. With M≥NM\ge N: 10011011−0100111010011011-01001110 (155−78155-78) =01001101=01001101 (7777), no borrow.

Complements

Definition. For NN on nn bits: one's complement =(2n−1)−N=(2^n-1)-N, i.e. every bit inverted; two's complement =2n−N=2^n-N, i.e. one's complement plus 1.

N=1011001N=1011001 (7 bits): one's complement 01001100100110, two's complement 01001110100111. Also 0001111→1110000→11100010001111\to1110000\to1110001; 101100→010011→010100101100\to010011\to010100. The complement of the complement returns NN; the two's complement of 00 is 00 (carry discarded).

Subtraction with two's complement. M−N=M+(2n−N)−2nM-N=M+(2^n-N)-2^n: add to MM the two's complement of NN and remove 2n2^n.

  • If M≥NM\ge N the sum produces a carry out (the 2n2^n term), discarded: M−NM-N remains.
  • If M<NM<N there is no carry out and the sum is 2n−(N−M)2^n-(N-M), 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 (00 positive, 11 negative) is prefixed. With 4 bits the range is −7-7 to +7+7 but zero has two representations (00000000 and 10001000); operations treat sign and magnitude separately, so subtraction needs a sign correction.

Two's complement. Positives are as unsigned (00 then the magnitude); a negative −N-N is the two's complement of NN, i.e. 2n−N2^n-N. MSB =0=0 for positives, 11 for negatives.

bits unsigned sign-magnitude two's complement
0000 0 +0+0 0
0111 7 7 7
1000 8 −0-0 −8-8
1001 9 −1-1 −7-7
1010 10 −2-2 −6-6
1011 11 −3-3 −5-5
1100 12 −4-4 −4-4
1101 13 −5-5 −3-3
1110 14 −6-6 −2-2
1111 15 −7-7 −1-1

In two's complement there is one zero and the range is asymmetric: −2n−1-2^{n-1} to 2n−1−12^{n-1}-1 (8 bits: −128-128 to 127127; −(−128)-(-128) is not representable). The value is read giving the MSB the weight −2n−1-2^{n-1}: 11110011=−128+64+32+16+2+1=−1311110011=-128+64+32+16+2+1=-13; 4 bits: 1011=−8+2+1=−51011=-8+2+1=-5.

8-bit examples: +9=00001001+9=00001001; −9-9: sign-magnitude 1000100110001001, two's complement 1111011111110111 (invert 00001001→1111011000001001\to11110110, add 1); −13=11110011-13=11110011; −3=11111101-3=11111101. Sign extension: replicate the MSB (1101→111111011101\to11111101 is still −3-3). 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
+6+13+6+13 00000110+0000110100000110+00001101 0001001100010011 +19+19
−6+13-6+13 11111010+0000110111111010+00001101 0000011100000111 +7+7
+6−13+6-13 00000110+1111001100000110+11110011 1111100111111001 −7-7
−6−13-6-13 11111010+1111001111111010+11110011 1110110111101101 −19-19

Subtraction. A−B=A+(−B)=A+B‾+1A-B=A+(-B)=A+\overline B+1 (one's complement of BB plus 1).

+7−(−12)+7-(-12): −12=11110100-12=11110100, its two's complement is 0000110000001100, and 00000111+00001100=00010011=+1900000111+00001100=00010011=+19. At 4 bits, 5−35-3: 0101+1100+1=(1) 0010=+20101+1100+1=(1)\,0010=+2, carry discarded. The adder-subtractor circuit that implements A±BA\pm B 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. 1000+1000=100001000+1000=10000 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 C=1C=1 if M≥NM\ge N (correct) and C=0C=0 if M<NM<N (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: AA and −B-B) 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, V=Cin, MSB⊕Cout, MSB.V=C_{in,\,MSB}\oplus C_{out,\,MSB}.

Why: two positives summing above 2n−1−12^{n-1}-1 send a carry into the sign bit (which becomes 1) but none out; two negatives below −2n−1-2^{n-1} give carry out but none in. At 4 bits 5+45+4: 0101+0100=10010101+0100=1001, carry into sign 11, out 00, V=1V=1 (10011001 would be −7-7).

8-bit examples (range −128…127-128\dots127):

  • 70+8070+80: 01000110+01010000=1001011001000110+01010000=10010110; carries 11 in, 00 out, V=1V=1 (read as −106-106).
  • (−70)+(−80)(-70)+(-80): 10111010+10110000=(1) 0110101010111010+10110000=(1)\,01101010; carries 00 in, 11 out, V=1V=1 (read as +106+106).
  • 100+27=127100+27=127: 0111111101111111, carries 0,00,0, V=0V=0 (at the limit, correct).
  • (−100)+(−28)=−128(-100)+(-28)=-128: (1) 10000000(1)\,10000000, carries 1,11,1, V=0V=0 (correct).

Reference exercises (two's complement, MSB = sign):

  • 110001+011101110001+011101 (6 bits, −15+29-15+29): sum 001110001110, carries 1,11,1: no overflow, +14+14.
  • 0110111+01011110110111+0101111 (7 bits, +55+47+55+47): sum 11001101100110, carries 11 in, 00 out: overflow (102>63102>63).
  • 00000111−1111010000000111-11110100 (+7−(−12)+7-(-12)): 00000111+00001100=0001001100000111+00001100=00010011, +19+19, no overflow.
  • 0110111−01011110110111-0101111 (+55−47+55-47, 7 bits): two's complement of the subtrahend 10100011010001; 0110111+1010001=(1) 00010000110111+1010001=(1)\,0001000; carries 1,11,1: no overflow, +8+8.

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 −(−2n−1)-(-2^{n-1}) is representable.
  • Extending a negative number with zeros instead of replicating the sign.
  • Writing −13-13 on 8 bits as 1000110110001101 (that is sign-magnitude; two's complement is 1111001111110011).

Versione ripasso

  • Unsigned subtraction: M≥NM\ge N: no borrow out, correct. M<NM<N: result M−N+2nM-N+2^n is wrong; magnitude =2n−=2^n- result, sign minus (01100100−10010110=1100111001100100-10010110=11001110, 256−206=50256-206=50, so −50-50).
  • Complements: one's =(2n−1)−N=(2^n-1)-N (invert bits); two's =2n−N==2^n-N= one's +1+1: 1011001→0100110→01001111011001\to0100110\to0100111.
  • Signed numbers: sign-magnitude has two zeros, symmetric range. Two's complement: one zero, range −2n−1…2n−1−1-2^{n-1}\dots2^{n-1}-1, MSB weight −2n−1-2^{n-1}: 11110011=−1311110011=-13; 4 bits 1011=−51011=-5. Sign extension replicates the MSB.
  • Sum: add including sign, discard carry out of the sign bit: +6−13=11111001=−7+6-13=11111001=-7; −6−13=11101101=−19-6-13=11101101=-19.
  • Subtraction: A−B=A+B‾+1A-B=A+\overline B+1, same adder: +7−(−12)=00010011=+19+7-(-12)=00010011=+19.
  • Unsigned overflow: in an addition iff carry out =1=1.
  • Signed overflow: V=Cin,MSB⊕Cout,MSBV=C_{in,MSB}\oplus C_{out,MSB}; operands of different sign never overflow; 70+80=1001011070+80=10010110, V=1V=1; −100−28=−128-100-28=-128, V=0V=0; +55+47+55+47 on 7 bits overflows.
  • 4-bit two's complement: 0111=70111=7, 1000=−81000=-8, 1001=−71001=-7, 1011=−51011=-5, 1111=−11111=-1. 8-bit: −9=11110111-9=11110111, −13=11110011-13=11110011.
  • Mistakes: carry out used as signed overflow; complement without +1+1; −13-13 written 1000110110001101; −(−2n−1)-(-2^{n-1}) taken as representable; zero extension of negatives.

Teoria collegata