Decimal 2 Binary
Decimal 2 Binary
Math 5630/6630
Decimal to Binary (Nothing to it!)
How would you go about representing a number x R in binary? We want
to have it in the form
d1 d2
x = (d0 .d1 d2 . . .)2 2e = d0 +
+ 2 + . . . 2e ,
2
2
for some integer e and digits di {0, 1}. Multiplying 2e into the sum gives
us
x = (d0 2e + d1 2e1 + d2 2e2 + . . .).
For now, assume x > 0 (we can always fix the sign later). To write x in
binary, all we need to do is
1. Find the highest power e of two so that 2e x. Subtract it from x to
form x1 = x 2e .
2. Now find the highest power of two that is smaller than x1 , then subtract it from x1 to form x2 .
3. Continue until you get 0 (youre done). If the floating point system
has finite accuracy, you may need to round/chop (maybe x has an
infinite binary expansion).
Example: x = 2.75.
1. First forget about the minus sign. Then we have 2.75.
2. 21 is the highest power of two less than 2.75. When we subtract it,
0.75 remains.
3. 20 > 0.75 is too big, but 21 = 0.5 is the next power and this one
works 21 < 0.75. Subtract it, 0.25 remains.
4. Next is 22 , which is exactly 0.25. Done!
So
2.75 = 1 21 + 0 20 + 1 21 + 1 22 .
To normalize, factor out the highest power of 2, i.e. 21 , which gives us
0
1
1
2.75 = 1 + + 2 + 3 21 .
2 2
2
Or in binary form: 2.75 = (1.0.11)2 21 .
1
Exercises:
Consider the normalized floating point system (2, 4, 2, 3), where the floating point approximation f l(x) of x is achieved by means of chopping. Represent the following numbers in this system. If the magnitude of the number
is bigger than the maximum floating point number, call it inf. If its
magnitude is smaller than the smallest positive number, call it 0.
1. x = 7.5
2. x = 20
3. x = = 3.14159265359 . . .