You are on page 1of 33

Fractional numbers

Floating point numbers


Tingting HU
07/03/2023
Power-of-2 multiply and division

Example: what’s the result of 1810 << 1, using 8-bit representation?

Ø 1810 << 1 is to multiply 1810 by 21 = 18*2 = 3610

Ø 1810 = 1610 + 210 = 24 +21 = 0001 00102


0001 00102 left shift by 1 position
à 0010 01002 = 25 + 22 = 3210 + 410 = 3610

25 + 22 = (24 + 21)*21
2
Power-of-2 multiply and division

Example: what’s the result of 1810 >> 1, using 8-bit representation?

Ø 1810 >> 1 is to divide 1810 by 21 = 18/2 = 910

Ø 1810 = 1610 + 210 = 24 +21 = 0001 00102


0001 00102 right shift by 1 position
à 0000 10012 = 23 + 20 = 810 + 110 = 910

23 + 20 = (24 + 21)/21
3
Power-of-2 multiply and division

Example: what’s the result of 1810 >> 2, using 8-bit representation?

Ø 1810 >> 2 is to divide 1810 by 22 = 18/4 = 410

Ø 1810 = 1610 + 210 = 24 +21 = 0001 00102


0001 00102 right shift by 2 position
à 0000 01002 = 22 = 410

4
Fractional binary numbers
Example: What’s the decimal value corresponding to 10.012. Show
your steps.
2i
2i-1

4
2
•••
1

bi bi-1 ••• b2 b1 b0 b-1 b-2 b-3 ••• b-j


1/2
1/4 •••
1/8

5
2-j
Fractional binary numbers
Example: What’s the decimal value corresponding to 10.012. Show
your steps.

10.012 = 1*21 + 0*20 + 0*2-1 + 1*2-2


= 2 + 0 + 0 + 0.25
= 2.2510

6
Fractional binary numbers
Example: Find the binary representation for 6.7510. Show your
steps.
2i
2i-1

4
2
•••
1

bi bi-1 ••• b2 b1 b0 b-1 b-2 b-3 ••• b-j


1/2
1/4 •••
1/8

2-j 7
Fractional binary numbers
Example: Find the binary representation for 6.7510. Show your
steps.
610 = 4 + 2
= 22+ 21
= 1102

0.7510 = 0.5 + 0.25


= 2-1 + 2-2
= 0.112

6.7510 = 110.112

8
Fractional binary conversion -- Algorithm
Algorithm to find binary representation for 2.62510.
2.62510 = 210 + 0.62510

The integer part:


210 = 102

The fractional part: 1) multiply by 2 and keep record of the resulting integer and
fractional part. 2) Repeat multiplying the fractional part by 2 until you get a
resulting fractional part equal to zero. 3) Then just assemble the integeral parts of
the results of each multiplication

0.625*21 = 1.2510 (1) 1 becomes 1st digit after the decimal point
0.25*21 = 0.510 (0) 0 becomes 2nd digit after the decimal point
0.5*21 =1.010. (1) 1 becomes 3rd digit after the decimal point
à 0.62510 = 0.1012

2.62510 = 10.1012 9
Fractional binary numbers
Example: Convert 6.7510 to binary use the algorithm introduced in
the previous slide. Show your steps.

6.7510 = 610 + 0.7510

The integer part:


610 = 1102

The fractional part:


0.75*2 = 1.510 (1) 1 becomes 1st digit after the decimal point
0.5 *2 = 1.010. (1) 1 becomes 2nd digit after the decimal point
à 0.7510 = 0.112

6.7510 = 110.112
10
Fractional binary conversion -- Algorithm
Algorithm explanation:
0.62510 = 0.xyz2
= x*2-1 + y*2-2 + z* 2-3

0.62510*2 = (x*2-1 + y*2-2 + z*2-3)*2 = x*20 + y*2-1 + z*2-2


1.25 = x*20 + y*2-1 + z* 2-2 à integer part is the value of x à x=1;
fractional part 0.25 = y*2-1 + z* 2-2

0.25*2 = (y*2-1 + z* 2-2 )*2 = y*20 + z*2-1


0.5 = y*20 + z*2-1 à integer part is the value of y à y=0;
fractional part 0.5 = z* 2-1

0.5*2 = (z* 2-1)*2 = z* 20


1.0 = z* 20 à integer part is the value of z à z=1;
fractional part is 0 à algorithm terminates

0.62510 = 0.1012 11
0 000 0000 0 0000 0000 0000 0000 0000 000 0
s exp frac
Minimum
0 000 0000 0 0000 0000 0000 0000 0000 001 denormalized
exp = value
0000 0000
and s exp frac


frac ≠ Maximum
0000 … 0000 0 000 0000 0 1111 1111 1111 1111 1111 111 denormalized
value
s exp frac
In the
Minimum
0 000 0000 1 0000 0000 0000 0000 0000 000 Normalized value
positive
exp ≠
0000 0000
range
and
s exp frac

exp ≠ Maximum
1111 1111 0 111 1111 0 1111 1111 1111 1111 1111 111 Normalized value

s exp frac
0 111 1111 1 0000 0000 0000 0000 0000 000 +∞
s exp frac
0 111 1111 1 Any non-zero bit sequence NaN
s exp frac 12
Floating point numbers

Example: what does 0x0FF00000 represent: normalized value, denormalized


value, infinity, NaN? Explain why.

13
Floating point numbers
Example: what does 0x0FF00000 represent: normalized value, denormalized
value, infinity, NaN? Explain why.

0FF00000 0 000 1111 1 1110 0000 0000 0000 0000 000


s exp frac
exp ≠ 0 and exp ≠255 à 0x0FF00000 represents a normalized value

14
Exercise

15
Power-of-2 multiply and divide

Ex. 1: what will be the results for 3510 << 1, 3510 >> 2? Represent the
results in both binary and decimal format. Show your steps.

Hint: first translate 3510 into binary format, then apply the power-of-2
multiply/divide. You will get the binary result, then translate it into
decimal.

16
Fractional binary numbers
Ex. 2: What’s the decimal value corresponding to 111.1012. Show
your steps.
2i
2i-1

4
2
•••
1

bi bi-1 ••• b2 b1 b0 b-1 b-2 b-3 ••• b-j


1/2
1/4 •••
1/8

17
2-j
Fractional binary numbers
Ex. 3: Find the binary representation for 30.87510. Show your steps.

2i
2i-1

4
2
•••
1

bi bi-1 ••• b2 b1 b0 b-1 b-2 b-3 ••• b-j


1/2
1/4 •••
1/8

2-j 18
Fractional binary numbers
Ex. 4: prove that 1.0112 << 1 = 10.112. Show your steps.

2i
2i-1

4
2
•••
1

bi bi-1 ••• b2 b1 b0 b-1 b-2 b-3 ••• b-j


1/2
1/4 •••
1/8

19
2-j
Fractional binary numbers
Ex. 5: prove that 101.12 >> 1 = 10.112. Show your steps.

2i
2i-1

4
2
•••
1

bi bi-1 ••• b2 b1 b0 b-1 b-2 b-3 ••• b-j


1/2
1/4 •••
1/8

20
2-j
Fractional binary numbers
Ex. 6: what is your observation about left or right shifted operation
for fractional binary numbers, based on the two examples seen
before, namely 1.0112 << 1 = 10.112 and 101.12 >> 1 = 10.112.

21
Fractional binary numbers
Ex. 7: Convert 95.12510 to binary use the algorithm introduced in
the first part. Show your steps. i
2
2i-1

4
2
•••
1

bi bi-1 ••• b2 b1 b0 b-1 b-2 b-3 ••• b-j


1/2
1/4 •••
1/8

2-j
22
0 000 0000 0 0000 0000 0000 0000 0000 000 0
s exp frac
Minimum
0 000 0000 0 0000 0000 0000 0000 0000 001 denormalized
exp = value
0000 0000
and s exp frac


frac ≠ Maximum
0000 … 0000 0 000 0000 0 1111 1111 1111 1111 1111 111 denormalized
value
s exp frac
In the
Minimum
0 000 0000 1 0000 0000 0000 0000 0000 000 Normalized value
positive
exp ≠
0000 0000
range
and
s exp frac

exp ≠ Maximum
1111 1111 0 111 1111 0 1111 1111 1111 1111 1111 111 Normalized value

s exp frac
0 111 1111 1 0000 0000 0000 0000 0000 000 +∞
s exp frac
0 111 1111 1 Any non-zero bit sequence NaN
s exp frac 23
Floating point numbers

Ex. 8: Among the following numbers represented in hexadecimal, which is


or are NaN? Explain why?
a) 0x7F800000
b) 0x7FA00000
c) 0xFF800000
d) 0xFF900000

24
Solutions

25
Power-of-2 multiply and divide
Ex. 1: what will be the results for 3510 << 1, 3510 >> 2? Represent the
results in both binary and decimal format.

Answer:
3510 = 3210 + 310 = 0010 00112

35 << 1: 0010 00112 << 1 = 0100 01102 = 64 + 4 + 2 = 7010 = 35 * 21


35 >> 2: 0010 00112 >> 2 = 0000 10002 = 810 = 35 // 22

26
Fractional binary numbers
Ex. 2: What’s the decimal value corresponding to 111.1012. Show
your steps.

111.1012 = 1*22 + 1*21 + 1*20 + 1*2-1 + 0*2-2 + 1* 2-3


= 4 + 2 + 1 + 0.5 + 0 + 0.125
= 7.62510

27
Fractional binary numbers
Ex. 3: Find the binary representation for 30.87510. Show your steps.

3010 = 16 + 8 + 4 + 2
= 24 + 23 + 22+ 21
= 111102

0.87510 = 0.5 + 0.25 + 0.125 0.87510 * 2 = 1.7510 (1)


= 2-1 + 2-2 + 2-3 0.7510 * 2 = 1.510 (1)
= 0.1112 0.510 * 2 = 1.010 (1)
à 0.87510 = 0.1112
30.87510 = 11110.1112

28
Fractional binary numbers
Ex. 4: prove that 1.0112 << 1 = 10.112. Show your steps.

method 1:
1.0112 = 1.37510
1.0112 << 1 = 1.37510 * 21 = 2.7510 = 10.112

method 2:
1.0112 << 1 = (1*20 + 0*2-1 + 1*2-2 + 1* 2-3) * 21
= 1*21 + 0*20 + 1*2-1 + 1* 2-2
= 10.112

29
Fractional binary numbers
Ex. 5: prove that 101.12 >> 1 = 10.112. Show your steps.

method 1:
101.12 = 5.510
101.12 >> 1 = 5.510 / 21 = 2.7510 = 10.112

method 2:
101.12 >> 1 = (1*22 + 0*21 + 1*20 + 1* 2-1) / 21
= 1*21 + 0*20 + 1*2-1 + 1* 2-2
= 10.112

30
Fractional binary numbers
Ex. 6: what is your observation about left or right shifted operation
for fractional binary numbers, based on the two examples seen
before, namely 1.0112 << 1 = 10.112 and 101.12 >> 1 = 10.112.

left shifted op. : move the binary point to the right by N positions;

right shifted op. : move the binary point to the left by N positions

31
Fractional binary numbers
Ex. 7: Convert 95.12510 to binary use the algorithm introduced in
the first part. Show your steps.

9510 = 64 + 31
= 26 + 24 + 23 + 22 + 21 + 20
= 101 11112
0.12510 * 2 = 0.2510 (0)
0.2510 * 2 = 0.510 (0)
0.510 * 2 = 1.010 (1)
à 0.12510 = 0.0012
95.12510 = 1011111.0012

32
Floating point numbers
Ex. 8: Among the following numbers represented in hexadecimal, which is
or are NaN? Explain why?
a) 0x7F800000 +∞
b) 0x7FA00000 7F800000 0 111 1111 1 0000 0000 0000 0000 0000 000

c) 0xFF800000 s exp frac


NaN
d) 0xFF900000 7FA00000 0 111 1111 1 0100 0000 0000 0000 0000 000
s exp frac
-∞
FF800000 1 111 1111 1 0000 0000 0000 0000 0000 000
s exp frac
NaN
FF900000 1 111 1111 1 0010 0000 0000 0000 0000 000
s exp frac
33

You might also like