Fixed Point Place Values
Before we begin, we are going to switch to using 16 bits, 8 bits doesn’t give us enough range for our numbers as you’ll see in a moment. Also, to make this easier to explain we are working with unsigned integers. This works equally fine with 2s complement signed values, and in fact they are what you should use. With signed 2s complement 8 bit values the range will be +127 / -128.
Instead of deciding the bits in our bytes follow this pattern:
32768 | 16384 | 8192 | 4096 | 2048 | 1024 | 512 | 256 | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
x | x | x | x | x | x | x | x | x | x | x | x | x | x | x | x |
What if we chose to use the first 8 bits as the whole number, and the last 8 bits as our fractional part?
The headings on the columns would now be this:
(The point column doesn’t exist, it is just here to make it more obvious what is going on. This is still a 16 bit number)
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | . | 1/2 | 1/4 | 1/8 | 1/16 | 1/32 | 1/64 | 1/128 | 1/256 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
x | x | x | x | x | x | x | x | . | x | x | x | x | x | x | x | x |
Now we can represent certain fractional numbers by working out which bits to turn on. We’ve turned our 16 bits into an 8 bit number that has 256 smaller values.
A half would be:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | . | 1/2 | 1/4 | 1/8 | 1/16 | 1/32 | 1/64 | 1/128 | 1/256 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | . | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
12.0625 would be:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | . | 1/2 | 1/4 | 1/8 | 1/16 | 1/32 | 1/64 | 1/128 | 1/256 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | . | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
What about values where the fractional part is not an even power of two? What about a simple number like 10.1?
10 decimal in binary is 00001010
, but how do we convert the fractional part? Read on to find out how to convert floating point values to their fixed point representations.