Docsity

Converting Floating Point Numbers to IEEE Binary Representation, Study notes of Operating Systems

Toggle sidebar
bg1
1
Floating Point Techniques
Dr. Thomas Way
Question: How can I convert a floating point number into the IEEE binary floating point
representation? For example, 13.8125.
Answer: Follow these steps.
Step 1: Express as a binary number.
For the part to the left of the decimal point, use the trick of repeatedly dividing by 2,
writing each remainder (either 1 or 0) as the next bit (from right to left), until all you have
left is 0, like this:
13 / 2 = 6 r 1 1
6 / 2 = 3 r 0 01
3 / 2 = 1 r 1 101
1 / 2 = 0 r 1 1101
0 (so we are done)
For the fraction part to the right of the decimal point, use a similar trick of dividing by ½,
which is the same as multiplying by 2. Here’s why we divided by 2 above, but will now
multiply by 2 here.
Decimal: 329.125 = 3x102 + 2x101 + 9 x100 + 1x10-1 + 2x10-2 + 5x10-3
Same as: = 3x100 + 2x10 + 9x1 + 1x1/10 + 2x1/100 + 5x1/1000
Binary: 101.011 = 1x22 + 0x21 + 1x20 + 0x2-1 + 1x2-2 + 1x2-3
Same as: = 1x4 + 0x2 + 1x1 + 0x1/2 + 1x1/4 + 1x1/8
So, you can see that everything to the left of the binary point is factored by powers of 2,
while everything to the right of the binary point is factored by powers of ½. When we
convert from decimal to binary, we are simply factoring out all of the powers of 2 (or ½)
to get the equivalent binary number.
Let’s convert 0.8125 by repeatedly multiplying by 2, and removing the 1 (or 0) that will
be on the left side of the binary point, writing each of them as our bits (this time writing
them from left to right), until you have 0 OR you fill up the available number of bits (in
the IEEE format we can have 23 bits):
.8125 x 2 = 1.625 1
.625 x 2 = 1.25 11
.25 x 2 = 0.5 110
.5 x 2 = 1.0 1101
.0 (so we are done)
So, the resulting binary number is: 1101.1101
pf2

Partial preview of the text

Download Converting Floating Point Numbers to IEEE Binary Representation and more Study notes Operating Systems in PDF only on Docsity!

Floating Point Techniques

Dr. Thomas Way

Question: How can I convert a floating point number into the IEEE binary floating point representation? For example, 13.8125.

Answer: Follow these steps.

Step 1 : Express as a binary number.

For the part to the left of the decimal point, use the trick of repeatedly dividing by 2, writing each remainder (either 1 or 0) as the next bit (from right to left), until all you have left is 0, like this: 13 / 2 = 6 r 1 1 6 / 2 = 3 r 0 01 3 / 2 = 1 r 1 101 1 / 2 = 0 r 1 1101 0 (so we are done)

For the fraction part to the right of the decimal point, use a similar trick of dividing by ½, which is the same as multiplying by 2. Here’s why we divided by 2 above, but will now multiply by 2 here.

Decimal: 329.125 = 3x10 2 + 2x10^1 + 9 x10^0 + 1x10-1^ + 2x10-2^ + 5x10- Same as : = 3x100 + 2x10 + 9x1 + 1x 1 / 10 + 2x^1 / 100 + 5x^1 / 1000

Binary: 101.011 = 1x2 2 + 0x2^1 + 1x2^0 + 0x2-1^ + 1x2-2^ + 1x2- Same as : = 1x4 + 0x2 + 1x1 + 0x 1 / 2 + 1x^1 / 4 + 1x^1 / 8

So, you can see that everything to the left of the binary point is factored by powers of 2, while everything to the right of the binary point is factored by powers of ½. When we convert from decimal to binary, we are simply factoring out all of the powers of 2 (or ½) to get the equivalent binary number.

Let’s convert 0.8125 by repeatedly multiplying by 2, and removing the 1 (or 0) that will be on the left side of the binary point, writing each of them as our bits (this time writing them from left to right), until you have 0 OR you fill up the available number of bits (in the IEEE format we can have 23 bits): .8125 x 2 = 1.625 1 .625 x 2 = 1.25 11 .25 x 2 = 0.5 110 .5 x 2 = 1.0 1101 .0 (so we are done)

So, the resulting binary number is: 1101.

Step 2 : determine sign bit

The number is positive number, so sign bit is 0

Step 3 : Determine the exponent by putting number in form 1.fffffff x 2exponent

1101.1101 x 2 0 (move the binary point 3 positions to the left) 1.1011101 x 2 3 (so the exponent is 3)

Step 4 : Add 127 to the exponent

3 + 127 = 130

Step 5 : Convert exponent to binary. Pad the left end with 0’s to get 8 bits, if necessary.

130 = 10000010

Step 6 : Determine the fraction part of the binary number we converted to in Step 3, by removing the leading 1.

.

Step 7 : Put the parts together (drop the decimal point).

0 10000010 10111010000000000000000

Step 8 : Double check your result, being sure to put the 1 in front of the fraction part:

1.1011101 x 2 130- 1.1011101 x 2 3 1101.1101 x 2 0 2 3 + 2 2 + 2 0 + 2 -1^ + 2 -2^ + 2 - 8 + 4 + 1 + ½ + ¼ + 1 / 13 + 0.5 + 0.25 + 0. 13 + 0.