1. Representing fractions
Another section has discussed how to represent whole numbers, both negative and positive in the form of the integer data type and 2's compliment.
Now the problem is how do we represent a fractional number such as $2 \frac 1 2$ (or in decimal, 2.5)?
You still have the same number of bits: 8, 16, 32 and so on in the CPU. And we can still represent the value of these in a table. But now some bits need to be used for fractions.
Let's assign half of the bits to represent integers, and half to represent fractions:
-8 | 4 | 2 | 1 | 1/2 | 1/4 | 1/8 | 1/16 |
---|---|---|---|---|---|---|---|
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
Working through this table is just like with any other binary number. You start with the smallest place value, on the right. In this case, a 1 here is equal to 1/16 (denary). A 1 in the next column left is equal to 1/8 (denary). And so on.
Just as described in the integers miniweb linked above, the leftmost value is given a negative sign.