Representation of fractional numbers on Hardware – The fixed point representation
In the hardware world, a number can be represented only by ones and zeros. How do we represent fractional numbers if that’s the case? How do we represent negative numbers in Hardware? While designing hardware systems, engineers often overlook the range and end up losing some of the bits especially the most significant bits due to overflow, resulting in inconsistent results in the simulation.Therefore, it’s important for DSP Engineers and Hardware designers to know about different ways to represent the fractional numbers.
Fractional numbers can be represented in different ways like Fixed point representation, Unsigned fixed point representation, IEEE754 representation etc.
The current blog-post discusses in detail about the Fixed point representation of integers, fractional numbers and negative numbers.
Binary representation of a number
The number 45 can be represented in binary as (101101)2$$ (2^0 \times 1) + (2^1 \times 0) + (2^2 \times 1) + (2^3 \times 1) + (2^4 \times 0) + (2^5 \times 1) \newline = 1 + 4 + 8 + 32 = 45_{10} $$
Binary representation of fractional numbers
Let’s take the example of (1011.01)2$$ = (2^{-1} \times 0) + ( 2^{-2} \times 1) + (2^{0} \times 1) + (2^{1} \times 1) + (2^{2} \times 0) + (2^{3} \times 1) \newline = 0 + 0.25 + 1+2 + 8 \newline = 11.25_{10} $$
Representation of a negative numbers – the two’s complement representation.
Let’s take the example of 8 bit two’s complement representation.
$$ -45_{10} = 11010011_{2} $$
The two complement of a number can be calculated as follows:
Write the binary representation of the number $$ 00101101_2 $$
Invert the bits, 0 becomes 1 and 1 becomes 0. $$ 11010010_2 $$
Add 1 to the bits inverted $$ 11010011_2 $$
Fixed point representation
Representation of fixed point number, the width of the number and the binary point position of the number. The fixed point numbers are represented using two parameters, the width of the number representation and the binary point position within the number. We use the notation fixedRepresentation of negative numbers in fixed point representation, the 2’s compliment
2’s compliment is used in representing numbers in the fixed point notation. $$ -45_{10} = 11010011_2 \\ fixed<8,0> = 11010011_2 \\ $$Similarly -11.25 can be represented as $$ fixed<8,2> = (11010011)2 $$