display | more...

Instructions on how to convert integers from decimal format to binary format can be found in many places, including the binary node. Many scientific calculators will do this conversion for you as well. What is less well known is how to convert to binary format numbers which include decimal places. Many calculators that will happily convert decimal integers to binary will simply truncate the number at the decimal point when asked to perform this operation. The technique is really not very difficult, however.

The remainder of this article will assume that the reader knows how to convert integers to binary format.

In decimal notation, numbers to the left side of the radix point (radix point being the generic-base term for decimal point) increase in value by factors of ten for each place moved over. Numbers to the right side decrease in value by factors of ten. Therefore, a number such as 242.75 is equivalent to:
200 + 40 + 2 + 7/10 + 5/100

Binary notation works in a similar manner, except numbers to the left increase in value by factors of two while numbers to the right decrease by factors of two. 1111 0010.11 is equivalent to:
128 + 64 + 32 + 16 + 2 + 1/2 + 1/4
Which is also equal to 242.75 in decimal notation.

We see in this example that the first number to the right of the radix point is 1/10, or one-tenth, in decimal notation, and 1/2, or one-half, in binary notation. This of course extends to the other bases as well. In octal notation it is 1/8, in hexadecimal it is 1/16, etc. Continuing to the right, we continue to decrease by powers of two. The second place is 1/4, third 1/8, fourth 1/16, etc.

Basic Technique

Unfortunately, this means that only decimals that can be expressed as fractional powers of two can be perfectly expressed in binary notation. For example, 0.21875, 0.375, and 0.1875 are all perfectly expressible in binary notation:

0.21875 = 7/32 = 1/8 + 1/16 + 1/32 = 0.00111
0.375   = 3/8  = 1/4 + 1/8         = 0.011
0.1875  = 5/16 = 1/4 + 1/16        = 0.0101

In case you didn't follow that, I'll break it down step by step. Pull the fraction apart one piece at a time and reduce what's left until your result has a 1 in the numerator.

7/32 = 1/32 + 6/32
6/32 = 3/16 = 1/16 + 2/16
2/16 = 1/8
This gives us 1/8 + 1/16 + 1/32, or put more basically,
0/2 + 0/4 + 1/8 + 1/16 + 1/32: 0.00111

A More Generalized Algorithm

But as I said, this only works when your fraction's denominator is a power of two. The number 0.1, for example, cannot be perfectly expressed in binary notation. 1/10 is not a fraction with a power of two in the denominator. The best we can do is come up with an approximation for 1/10 in binary notation. Before we tackle this problem, let's work up a better algorithm for real numbers that can be expressed perfectly in binary.

A more comprehensive and direct technique would be to examine the fraction as we proceed right of the point and see if we can pull a 1/2, a 1/4, a 1/8, etc. out of it as we move along. For example, 0.21875 is smaller than 1/2, so we put a 0 in the first place. It's smaller than 1/4, so we put a 0 in the second place. It's larger than 1/8, so we put a 1 in the third place and pull out that 1/8 (0.125) from the fraction (leaving 0.09375). We do the same with 1/16 (leaving 0.03125) and 1/32 (leaving 0) to get our answer: 0.00111. Cleaning this up in a nice, column format:

7/32 < 1            → 0.
7/32 < 1/2  (16/32) → 0
7/32 < 1/4  (8/32)  → 0
7/32 > 1/8  (4/32)  → 1 (remove 1/8 from the fraction)
3/32 > 1/16 (2/32)  → 1 (remove 1/16 from the fraction)
1/32 = 1/32         → 1 (finished)

Answer: 0.00111

All that subtraction and division is hard to do though. We can simplify the algorithm if we multiply the fraction by 2 at each step in the process. What does this do? It means we can make a much simpler test at each step: Is the fraction greater than or equal to 1, rather than is it greater than or equal to 1/2n. Multiplying the first step by 2 means we test for 1 instead of 1/2, then multiplying by another 2 at step two means we test for 1 instead of 1/4, etc. When the fraction is greater than one, pull the one out and put a 1 in that place after the point; and when the fraction is equal to one, the algorithm terminates.

           5/16 < 1 → 0.
2 × 5/16 = 5/8  < 1 → 0
2 × 5/8  = 5/4  > 1 → 1 (remove 1, or 4/4, from the fraction)
2 × 1/4  = 1/2  < 1 → 0
2 × 1/2  = 1    = 1 → 1 (finished)

Our answer is 0.0101

Imperfectly Expressible Numbers

So this seems to work pretty well, at least for numbers evenly expressible in terms of x/2n. Let's run it against a number which is not expressible in this manner, 0.1, or 1/10.

           1/10 < 1 → 0.
2 × 1/10 = 1/5  < 1 → 0
2 × 1/5  = 2/5  < 1 → 0
2 × 2/5  = 4/5  < 1 → 0
2 × 4/5  = 8/5  > 1 → 1
2 × 3/5  = 6/5  > 1 → 1
2 × 1/5  = 2/5

At this point we've come back around to 2/5, so we see the algorithm has entered into a loop beginning at step 3. This means that we have a repeating value, much like 1/3 is a repeating value in decimal notation. Our answer is
   ____
0.00011

Incidentally, this doesn't have to be performed using fractions:

            0.375 < 1 → 0.
2 × 0.375 = 0.75  < 1 → 0
2 × 0.75  = 1.5   > 1 → 1
2 × 0.5   = 1     = 1 → 1

Answer: 0.011

Now you can get to work on converting pi to binary.

Scientific Notation

One further point of interest is scientific notation. Recall that scientific notation in decimal takes a form such as 2.1 × 103 to represent 2,100. Scientific notation in decimal uses a 10 to represent the shifting of the radix point because each subsequent position in the number represents a power of ten.

Likewise, binary notation would use a 2. So a number such as 1 1000 0000 (384 in decimal) could be written as 1.1 × 101000. Don't be fooled by that 10It's actually a binary 2!

Log in or register to write something here or to contact authors.