3.3 Number representations *¶
Tip
In this book, an asterisk (*) marks an optional section. If you have limited time or find one difficult, you can skip it for now and return after finishing the required sections.
3.3.1 Sign and magnitude, ones' complement, and two's complement¶
The table in the previous section shows that each signed integer type represents one more negative number than positive numbers. For example, the range of byte is \([-128, 127]\). To understand this surprising asymmetry, we need to look at sign and magnitude, ones' complement, and two's complement.
First, computers store signed integers in two's complement form. Before exploring why, let's define the three representations.
- Sign and magnitude. The highest bit is the sign bit: \(0\) indicates a positive sign and \(1\) a negative sign. The remaining bits give the magnitude.
- Ones' complement. For a positive number, this is the same as sign and magnitude. For a negative number, start with sign and magnitude and invert all bits except the sign bit.
- Two's complement. For a positive number, this is again the same as sign and magnitude. For a negative number, add \(1\) to its ones' complement representation.
The figure shows how to convert between sign and magnitude, ones' complement, and two's complement.
Figure 3-4 Converting between sign and magnitude, ones' complement, and two's complement
Description of Figure 3-4: Eight-bit sign and magnitude, ones' complement, and two's complement
Compare the conversions for positive and negative numbers to see why two's complement has only one representation of zero.
Three rows show sign and magnitude, ones' complement, and two's complement for +0, +5, −0, and −28. Each entry is an 8-bit binary number in groups of four bits. Two-way arrows link sign and magnitude to ones' complement, then ones' complement to two's complement. The text on the right gives the forward conversion rules for negative numbers.
| Number | Sign and magnitude | Ones' complement | Two's complement |
|---|---|---|---|
| +0 | 0000 0000 | 0000 0000 | 0000 0000 |
| +5 | 0000 0101 | 0000 0101 | 0000 0101 |
| −0 | 1000 0000 | 1111 1111 | 0000 0000 |
| −28 | 1001 1100 | 1110 0011 | 1110 0100 |
The highest bit is the sign bit. For +0 and +5, all three representations agree. For a negative number, keep the high bit 1 and invert the low seven bits to get ones' complement. Add 1 and keep the low eight bits to get two's complement. The table applies this rule to −28. To go back, subtract 1 from the two's complement value, then keep the sign bit and invert the low seven bits to recover sign and magnitude. For −0, adding 1 to its ones' complement, 1111 1111, gives the nine-bit result 1 0000 0000. Discarding the carry leaves 0000 0000, the same as +0, so the original positive and negative zeros can no longer be distinguished.
Sign and magnitude is the most intuitive representation, but it has limitations. For one, the sign-and-magnitude bit pattern of a negative number cannot be used directly in ordinary binary arithmetic. For example, adding the sign-and-magnitude bit patterns for \(1 + (-2)\) gives \(-3\), which is clearly incorrect.
To address this, computers introduced ones' complement. Convert the operands from sign and magnitude to ones' complement, compute \(1 + (-2)\) in ones' complement, and convert the result back to sign and magnitude. This gives the correct result, \(-1\).
Another limitation is that sign and magnitude has two representations of zero, \(+0\) and \(-0\). Two different bit patterns for zero can introduce ambiguity. For example, a conditional test may give the wrong result if it does not account for positive and negative zero. Adding special checks to handle both representations can reduce computational efficiency.
Ones' complement shares this ambiguity, which is why computers went on to adopt two's complement. Consider the conversion of negative zero from sign and magnitude to ones' complement and then to two's complement:
Adding \(1\) to negative zero in ones' complement produces a carry. Since a byte is only 8 bits wide, the \(1\) that spills into the ninth bit is discarded. Thus the two's complement representation of negative zero is \(0000 \; 0000\), the same as for positive zero. Two's complement has only one zero, resolving the ambiguity.
One question remains. The range of byte is \([-128, 127]\), so where does the extra negative number \(-128\) come from? Every integer in the closed interval \([-127, +127]\) has a sign-and-magnitude, ones' complement, and two's complement representation, and we can convert between sign and magnitude and two's complement.
However, the two's complement bit pattern \(1000 \; 0000\) has no sign-and-magnitude counterpart. Applying the conversion procedure would give the sign-and-magnitude pattern \(0000 \; 0000\). This is a contradiction: that pattern represents \(0\), whose two's complement representation should be identical to itself. Computers assign the special two's complement pattern \(1000 \; 0000\) to \(-128\). In fact, adding \((-1) + (-127)\) in two's complement gives exactly \(-128\).
Notice that every calculation above is an addition. This points to an important fact: addition is central to the design of computer arithmetic circuits. Compared with multiplication, division, or subtraction, addition is simpler to implement in hardware, easier to parallelize, and faster to execute.
Computers can, of course, do more than add. Combining addition with basic logical operations lets them perform many other mathematical operations. For example, subtraction \(a - b\) can be rewritten as addition \(a + (-b)\), and multiplication and division can be expressed as repeated addition or subtraction.
We can now see why computers use two's complement. The same circuitry and operations can add positive and negative numbers, with no need for special subtraction circuitry or special handling of positive and negative zero. This greatly simplifies hardware design and improves computational efficiency.
There is more to the design of two's complement than we have room to explore here. Interested readers can investigate it further.
3.3.2 Floating-point representation¶
Both int and float occupy 4 bytes, so why is the range of float so much larger than that of int? At first glance, having to represent fractions as well might seem to give float a smaller range.
The difference lies in how a float represents numbers. Write a 32-bit binary number as follows:
Under IEEE 754, a 32-bit float consists of three fields.
- Sign \(\mathrm{S}\): 1 bit, \(b_{31}\).
- Exponent \(\mathrm{E}\): 8 bits, \(b_{30} b_{29} \ldots b_{23}\).
- Fraction \(\mathrm{N}\): 23 bits, \(b_{22} b_{21} \ldots b_0\).
The binary representation of a float gives its value as follows:
Expressed in decimal terms, the formula becomes:
The terms have the following ranges:
Figure 3-5 Calculating the value of an IEEE 754 float
Description of Figure 3-5: Bit fields of a single-precision floating-point number
Split the 32-bit encoding into sign, exponent, and fraction, then calculate its value.
This IEEE 754 single-precision number occupies 4 bytes. The 32 cells in the diagram run from bit 31 on the left to bit 0 on the right. Braces mark the sign S in orange, the exponent E in blue, and the fraction N in green.
The table first lists the fields and their stored values. Bit positions are numbered from 0 at the right. The labels b31, b30, b23, b22, and b0 mark the corresponding field boundaries.
| Field | Bit positions | Number of bits | Binary value in this example | Interpretation |
|---|---|---|---|---|
| Sign S | Bit 31 | 1 | 0 | Positive sign |
| Exponent E | Bits 30 through 23 | 8 | 01111100 | The stored exponent is 124 in decimal |
| Fraction N | Bits 22 through 0 | 23 | 01100000000000000000000 | The first three bits are 011, followed by 20 zeros |
E is between 1 and 254, so use the formula for a normal number. The sign factor is +1. The actual exponent is 124 − 127 = −3, so the scale factor is 2 to the power of −3, or 0.125. In the fraction field, only the second and third bits after the binary point are 1. Thus N = 2 to the power of −2 plus 2 to the power of −3, or 0.375. Adding the implicit leading 1 gives 1.375. Multiplying the three factors gives 1 × 0.125 × 1.375 = 0.171875, matching the result at the lower right of the figure.
The formula for a normal number is (−1)^S × 2^(E−127) × (1+N). Values with E equal to 0 or 255 follow special rules for zero, subnormal numbers, infinity, and NaN, so this formula does not apply directly. The exponent increases range without increasing precision. A normal number has 23 fraction bits plus the implicit 1, for 24 bits of binary precision. As the absolute value increases, the spacing between adjacent representable numbers generally grows.
In the example shown above, \(\mathrm{S} = 0\), \(\mathrm{E} = 124\), and \(\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\). Substituting gives:
We can now answer the original question: the exponent field gives float a far greater range than int. From the calculation above, the largest positive value of float is \(2^{254 - 127} \times (2 - 2^{-23}) \approx 3.4 \times 10^{38}\). Flipping the sign bit gives the most negative value.
The greater range of float comes at the cost of precision. The integer type int uses all 32 bits to represent evenly spaced numbers. With float, the exponent means that as the magnitude grows, the spacing between adjacent representable numbers generally increases.
As the table shows, exponent values \(\mathrm{E} = 0\) and \(\mathrm{E} = 255\) have special meanings, allowing representations of zero, infinity, \(\mathrm{NaN}\), and other cases.
Table
| Exponent field E | Fraction field \(\mathrm{N} = 0\) | Fraction field \(\mathrm{N} \ne 0\) | Formula |
|---|---|---|---|
| \(0\) | \(\pm 0\) | Subnormal number | \((-1)^{\mathrm{S}} \times 2^{-126} \times (0.\mathrm{N})\) |
| \(1, 2, \dots, 254\) | Normal number | Normal number | \((-1)^{\mathrm{S}} \times 2^{(\mathrm{E} -127)} \times (1.\mathrm{N})\) |
| \(255\) | \(\pm \infty\) | \(\mathrm{NaN}\) |
Subnormal numbers provide much finer resolution near zero. The smallest positive normal number is \(2^{-126}\), while the smallest positive subnormal number is \(2^{-126} \times 2^{-23}\).
Double precision (double) uses a representation similar to float, so we will omit the details.