Salta al contenuto
Note per Studenti Signed Integer Representations Report

Signed Integer Representations

In questa pagina 6

With nn bits there are 2n2^n configurations. Unsigned integers range from 00 to 2n−12^n-1; to represent negatives a convention is chosen. In all conventions below the range is about [−2n−1,2n−1][-2^{n-1},2^{n-1}].

Sign-magnitude

Definition (sign-magnitude). The most significant bit is the sign (0 = positive, 1 = negative); the other n−1n-1 bits are the magnitude.

  • 8 bits: +5=0000 0101+5=0000\,0101, −5=1000 0101-5=1000\,0101. Range [−(2n−1−1), 2n−1−1][-(2^{n-1}-1),\,2^{n-1}-1] (−127…127-127\dots127).
  • Defects: two zeros (0000 00000000\,0000, 1000 00001000\,0000) and addition needs different circuits depending on signs.

One's complement

Definition (one's complement). The negative −x-x is obtained by inverting all bits of xx.

  • 8 bits: +5=0000 0101+5=0000\,0101, −5=1111 1010-5=1111\,1010. Same range as sign-magnitude, still two zeros (0000 00000000\,0000 and 1111 11111111\,1111).
  • In addition the carry out must be added back to the result (end-around carrycarry leaving the most significant bit and added to the least significant bit).

Two's complement

Definition (two's complement). The representation used by all processors. The most significant bit has negative weight: x=−cn−1 2n−1+∑i=0n−2ci 2i.x=-c_{n-1}\,2^{n-1}+\sum_{i=0}^{n-2}c_i\,2^i. Equivalently, a negative −x-x is represented by the unsigned number 2n−x2^n-x.

Example. 1110 11001110\,1100 on 88 bits is −128+64+32+8+4=−20-128+64+32+8+4=-20, and also 28−20=2362^8-20=236.

Property (opposite in two's complement). Invert all bits and add 1 (works both ways): +20=0001 0100 →invert 1110 1011 →+1 1110 1100=−20.+20=0001\,0100\ \xrightarrow{\text{invert}}\ 1110\,1011\ \xrightarrow{+1}\ 1110\,1100=-20.

Example. At 44 bits the opposite of 00110011 (+3+3) is 1100+1=11011100+1=1101 (−3-3).

Shortcut: from the right, copy the bits up to and including the first 1, then invert all the others (0001 0100‾→1110 1100‾0001\,0\underline{100}\to1110\,1\underline{100}).

From two's complement to decimal. If MSB = 0 read as unsigned. If MSB = 1 use the negative weight: 1110 1100=−128+64+32+8+4=−201110\,1100=-128+64+32+8+4=-20, or compute the opposite (0001 0100=200001\,0100=20) and prepend a minus.

Edge case: the opposite of −128=1000 0000-128=1000\,0000 on 8 bits is 1000 00001000\,0000 again: +128+128 is not representable.

Property (sign extension). To go from nn to m>nm>n bits, replicate the sign bit on the left: −20-20 on 8 bits 1110 11001110\,1100 becomes on 16 bits 1111 1111 1110 11001111\,1111\,1110\,1100; +20+20 becomes 0000 0000 0001 01000000\,0000\,0001\,0100. Needed, for instance, when a processor loads a signed byte into a 32-bit register.

Example. 11011101 (−3-3 on 44 bits) becomes 1111 11011111\,1101 on 88 bits, still −3-3.

Excess K (bias)

Definition (excess K). Store x+Kx+K as an unsigned integer; usually K=2n−1K=2^{n-1} or 2n−1−12^{n-1}-1.

4-bit comparison

Bits Unsigned Sign-magnitude One's compl. Two's compl. Excess 8
0000 0 +0 +0 0 −8
0011 3 +3 +3 +3 −5
0111 7 +7 +7 +7 −1
1000 8 −0 −7 −8 0
1011 11 −3 −4 −5 +3
1111 15 −7 −0 −1 +7

The same bit sequence has different meanings: the value depends on the convention used to read it.

Common mistakes

  • Computing the two's complement opposite by inverting without adding 1 (that gives one's complement).
  • Extending a negative number with zeros: 1110 1100→0000 0000 1110 11001110\,1100\to0000\,0000\,1110\,1100 is +236+236, not −20-20.
  • Forgetting the asymmetry: with 8 bits the minimum is −128-128, the maximum +127+127.

Versione ripasso

4 bits: 10111011 is 1111 unsigned, −3-3 sign-magnitude, −4-4 one's complement, −5-5 two's complement, +3+3 excess 8.

Common mistakes: inverting without +1+1; extending a negative with zeros (+236+236, not −20-20); forgetting asymmetry (−128⋯+127-128\dots+127).

Teoria collegata