Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you convert a fraction to binary?

Tags:

binary

People also ask

How do you convert a decimal to a binary?

Divide the integer by 2 successively while noting the quotient and remainder. Next, write the all the remainders in reverse order. In this example, remainders are 1, 1, 1, 1. So, writing in reverse order, the number 15 in decimal is represented as 1111 in binary.

How do you represent 0.3 in binary?

1 Answer. Hence, 0.3=(0.01001−−−−−)2 0.3 = ( 0.01001 - - - - - ) 2 .


In university I learned it this way:

  1. Multiply by two
  2. take decimal as the digit
  3. take the fraction as the starting point for the next step
  4. repeat until you either get to 0 or a periodic number
  5. read the number starting from the top - the first result is the first digit after the comma

Example:

0.1 * 2 = 0.2 -> 0
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
0.6 * 2 = 1.2 -> 1
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
0.6 * 2 = 1.2 -> 1
Result: 0.00011(0011) periodic.

 1              1
-- (dec)  =   ---- (bin)
10            1010


       0.000110011...
      -------------
1010 | 1.0000000000
         1010
       ------
         01100
          1010
         -----
          0010000
             1010
            -----
             01100
              1010
             -----
              0010

This may be somewhat confusing, but the decimal positions in binary would represent reciprocals of powers of two (e.g., 1/2, 1/4, 1/8, 1/16, for the first, second, third and fourth decimal place, respectively) just as in decimal, decimal places represent reciprocals of successive powers of ten.

To answer your question, you would need to figure out what reciprocals of powers of two would need to be added to add up to 1/10. For example:

1/16 + 1/32 = 0.09375, which is pretty close to 1/10. Adding 1/64 puts us over, as does 1/128. But, 1/256 gets us closer still. So:

0.00011001 binary = 0.09765625 decimal, which is close to what you asked.

You can continue adding more and more digits, so the answer would be 0.00011001...


Here is how to think of the method.

Each time you multiply by 2, you are shifting the binary representation of the number left 1 place. You have shifted the highest digit after the point to the 1s place, so take off that digit, and it is the first (highest, therefore leftmost) digit of your fraction. Do that again, and you have the next digit.

Converting the base of a whole number by dividing and taking the remainder as the next digit is shifting the number to the right. That is why you get the digits in the opposite order, lowest first.

This obviously generalizes to any base, not just 2, as pointed out by GoofyBall.

Another thing to think about: if you are rounding to N digits, stop at N+1 digits. If digit # N+1 is a one, you need to round up (since digits in binary can only be a 0 or 1, truncating with the next digit a 1 is as inaccurate as truncating a 5 in decimal).


Took me a while to understand @Femaref ('s) answer so thought I would elaborate.

Elboration

You want to convert decimal 1/10 which equal 0.1 to binary. Start with 0.1 as your input and follow these steps:

  1. Multiply input by 2 (mult column)
  2. Take decimal from answer (answer column) as the digit (binary column)
  3. Take the fraction (fraction column) as the input for the next step
  4. Repeat steps 1, 2 and 3 until you either get to 0 or a periodic number. The start of periodic number in this case is shown in last column so we can stop there. But I continued to show the repetition for clarity.
  5. The answer is the numbers taken from the binary column starting at the top.

In this case it is:

0.00011(0011) Note: numbers within parenthesis will keep repeating (periodic)

+-------+-------+--------+---------+----------+--------+----------------------+
| input | mult  | answer | decimal | fraction | binary |                      |
+-------+-------+--------+---------+----------+--------+----------------------+
|   0.1 |  2    |    0.2 |    0    |     .2   |      0 |                      |
|   0.2 |  2    |    0.4 |    0    |     .4   |      0 |                      |
|   0.4 |  2    |    0.8 |    0    |     .8   |      0 |                      |
|   0.8 |  2    |    1.6 |    1    |     .6   |      1 |                      |
|   0.6 |  2    |    1.2 |    1    |     .2   |      1 |                      |
|   0.2 |  2    |    0.4 |    0    |     .4   |      0 |                      |
|   0.4 |  2    |    0.8 |    0    |     .8   |      0 |                      |
|   0.8 |  2    |    1.6 |    1    |     .6   |      1 |                      |
|   0.6 |  2    |    1.2 |    1    |     .2   |      1 | < Repeats after this |
|   0.2 |  2    |    0.4 |    0    |     .4   |      0 |                      |
|   0.4 |  2    |    0.8 |    0    |     .8   |      0 |                      |
|   0.8 |  2    |    1.6 |    1    |     .6   |      1 |                      |
|   0.6 |  2    |    1.2 |    1    |     .2   |      1 |                      |
+-------+-------+--------+---------+----------+--------+----------------------+