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 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 *1) + (2^1 * 0) + (2^2 * 1) + (2^3 * 1) + (2^4*0) + (2^5 * 1)
= 1 + 4 + 8 + 32
= (45)10

 

Binary representation of fractional numbers:
Let’s take the example of (1011.01)2

= (2^-1 * 0) + ( 2^-2 * 1) + (2^0 * 1) + (2^1 * 1) + (2^2 * 0) + (2^3 * 1)

= 0 + 0.25 + 1+2+ 8
= (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 fixed<w,b> where w denotes the number of bits used and b denotes the binary point counting from the least significant bit.
Let’s take the example of 11.25 discussed above.

(11.25)10 = (001011.01)2
fixed<8,2> = (00101101)2

fixed<8,2> represents 8 bit fixed point number, where the two rightmost bits are fractional and 6 bits are for decimal representation.

(5.625)10 = (00101.101)2
fixed<8,3> = (00101101)2

The same bit pattern 00101101 can also be used to represent number 5.625, where 5 bits and 3 are decimal bits.

Integer numbers can be represented with 0 bits as fractional.

(45)10 = (00101101)2
fixed<8,0> = (00101101)2.

Representation 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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s