Signed Integer Representations
In questa pagina 6
With bits there are configurations. Unsigned integers range from to ; to represent negatives a convention is chosen. In all conventions below the range is about .
Sign-magnitude
Definition (sign-magnitude). The most significant bit is the sign (0 = positive, 1 = negative); the other bits are the magnitude.
- 8 bits: , . Range ().
- Defects: two zeros (, ) and addition needs different circuits depending on signs.
One's complement
Definition (one's complement). The negative is obtained by inverting all bits of .
- 8 bits: , . Same range as sign-magnitude, still two zeros ( and ).
- 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: Equivalently, a negative is represented by the unsigned number .
Example. on bits is , and also .
- Range , asymmetric (8 bits: ; 16 bits: ; 32 bits: about ).
- One zero only; MSB = 1 iff the number is negative.
- Addition is the same as for unsigned integers: one adder serves both (Binary ArithmeticBinary addition and two's complement subtraction, overflow for unsigned (carry) and signed (sign change) numbers, the N Z C V flags, multiplication by shifts and additions, Booth's algorithm with a worked example, division, and logical, arithmetic and rotate shifts.Binary Arithmetic →).
Property (opposite in two's complement). Invert all bits and add 1 (works both ways):
Example. At bits the opposite of () is ().
Shortcut: from the right, copy the bits up to and including the first 1, then invert all the others ().
From two's complement to decimal. If MSB = 0 read as unsigned. If MSB = 1 use the negative weight: , or compute the opposite () and prepend a minus.
Edge case: the opposite of on 8 bits is again: is not representable.
Property (sign extension). To go from to bits, replicate the sign bit on the left: on 8 bits becomes on 16 bits ; becomes . Needed, for instance, when a processor loads a signed byte into a 32-bit register.
Example. ( on bits) becomes on bits, still .
Excess K (bias)
Definition (excess K). Store as an unsigned integer; usually or .
- Excess 128 on 8 bits: ; ; .
- The order of the codes equals the order of the values: comparison is done as for unsigned numbers. For this reason it is used for the exponent in Fixed-Point and Floating-Point NumbersFixed-point numbers as an integer divided by 2 to the F with resolution and a sensor example, floating point in base 2, the IEEE 754 single and double formats, conversions in both directions with worked examples, special values and denormals, range, precision, rounding and floating-point operations.Fixed-Point and Floating-Point Numbers → (with ).
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: is , not .
- Forgetting the asymmetry: with 8 bits the minimum is , the maximum .
Versione ripasso
- Sign-magnitude: MSB sign, rest magnitude; range ; two zeros; , .
- One's complement: invert all bits; two zeros; end-around carry in addition.
- Two's complement: ; ; range (8 bits: ); one zero; MSB = 1 iff negative; one adder for signed and unsigned (Binary ArithmeticBinary addition and two's complement subtraction, overflow for unsigned (carry) and signed (sign change) numbers, the N Z C V flags, multiplication by shifts and additions, Booth's algorithm with a worked example, division, and logical, arithmetic and rotate shifts.Binary Arithmetic →).
- Opposite: invert and add 1 (); shortcut: copy up to the first 1, invert the rest. Opposite of is .
- Sign extension: replicate the sign bit ().
- Excess K: store unsigned ( or ); order preserved; used for the floating-point exponent (, Fixed-Point and Floating-Point NumbersFixed-point numbers as an integer divided by 2 to the F with resolution and a sensor example, floating point in base 2, the IEEE 754 single and double formats, conversions in both directions with worked examples, special values and denormals, range, precision, rounding and floating-point operations.Fixed-Point and Floating-Point Numbers →). Excess 128: .
4 bits: is unsigned, sign-magnitude, one's complement, two's complement, excess 8.
Common mistakes: inverting without ; extending a negative with zeros (, not ); forgetting asymmetry ().