You already know how to convert integer decimal numbers to the binary number system. Now it's time to learn how to convert decimal fractions to the binary!
Fractions
But first, let's remember what a fraction is. A fraction is a number that represents any number of equal parts of a whole – for example, , , , ... Here, the number above the horizontal line (the numerator) tells us how many equal parts are there, and the number below (the denominator) represents how many parts make up a whole. In the example below, the number represents three fourths of a cake.
Let's remember how fractions are written in the decimal number system. A decimal fraction is a fraction whose denominator is not written explicitly, but is implied to be a power of ten. The exact power is determined by the number of digits to the right of the decimal point.
For example, can be written in the decimal number system as (two digits after the decimal point), which is , or , or ,
(zero digits after the decimal point) is , or ,
(one digit after the decimal point) is , or , or ,
(three digits after the decimal point) is , or , or , and so on...
Any decimal number consists of two parts – the integer part, which represents how many "whole" numbers are there and is written before the decimal point, and the fractional part, which is written after the decimal point. For example, for the number the integer part is and is the fractional.
Now, let's look at this big long decimal fraction more closely and we'll see that in the decimal number system, the fractional part of a number behaves exactly like its decimal part!
(Don't worry if you don't know what is. Negative exponents are just a convenient way of writing: it means that the base number – in our case, – is on the different side of the fraction line. So, means , is , is , and so on.)
The binary number system works exactly the same, except here, we use instead of 10 as a basе. Which part of a number is the integer part and which is the fractional part does not depend on a number system – you can count cakes in different ways, but the whole cake won't turn into a part of the cake if you change your way of counting.
And now, you almost know how to convert decimal fractions to the binary system! First, we convert the integer part of a decimal number to the binary (which you already know how to do), then we convert to the binary its fractional part (which is a little bit trickier, but similar), and finally, we combine both parts into one.
Let's try with our big long decimal fraction:
Converting the integer part
The integer part is . Let's divide by (using integer division) and store the remainders until the result is zero like we did previously.
| Quotient | Remainder |
| 15 // 2 = 7 (15 = 2 7 + 1) | 1 |
| 7 // 2 = 3 (7 = 2 3 + 1) | 1 |
| 3 // 2 = 1 (3 = 2 1 + 1) | 1 |
| 1 // 2 = 0 (1 = 2 0 + 1) | 1 |
The resulting remainders array in reverse order will be our integer binary number. In our case, it's .
Or let's subtract the highest power of from until the result is zero. We start from the highest power of less than or equal to our number (in our case, it's ). For each power of , we write down , if we can subtract it from our resulting number, and otherwise.
| 8 | 4 | 2 | 1 |
| 1 | 1 | 1 | 1 |
So, is in the binary number system.
Converting the fractional part
But how to convert to the binary system since we are dealing with fractions? The algorithm is almost the same. We divide the fractional part of our number by (in other words, we multiply it by ) and store integer remainders until the fractional part of our resulting number is zero. Those remainders (not in the reverse order!) will be our binary fractional part, written after the decimal point. Let's try.
| Fractional part | Integer remainder |
| 0.375 2 = 0.75 | 0.75 |
| 0.75 2 = 1.5 | 1.5 |
| 1.5 - 1 = 0.5 0.5 2 = 1 |
1 |
- First, we multiply by . , the integer part of the result is , so we write down ;
- Then, we multiply by . , the integer part of the result is , so we write down . For the next iteration, we will need only the fractional part of our resulting number, which is ;
- And last, we multiply by . – in other words, there is no fractional part left, so the algorithm stops here. The integer part of the result is , which we write down.
So, in the binary number system is . Here is the algorithm step-by-step:
- multiply the number by ;
- get the fractional part for the next iteration;
- remember the integer remainder for the binary digit;
- repeat the steps until the fractional part is equal to ;
- write the remainders in the non-reverse order.
Let's confirm our result with the second algorithm – subtraction of powers of . But now we start from , and not from the highest power of less than or equal to our number.
| 0 | 1 | 1 |
And our result is still correct. The second algorithm is identical to that for the integer part of a number, except now we must start subtraction from .
And finally, we combine both parts of our resulting binary number. in decimal is in binary.
Conclusion
Today we've learned how to convert decimal fractions to the binary number system. Turns out, converting fractions is not much more complicated than converting regular integer numbers. You just need to convert both decimal and fractional parts of a number separately and then combine them into a new binary number!