# Converting from base $10$ to base $2$

I am attempting to solve the following two questions. The first question I believe I have done correctly and I am looking to confirm this answer. The second question I am not sure about.

$1)$ Convert $5.1$ in base $10$ to base $2$

My solution.

Looking at the whole number to the left of the decimal $5$

$$(5)_{10} = 2^2+1 = 2^2 + 2^0 = 1\cdot2^2 + 0\cdot 2^1 + 1 \cdot2^0 = (101)_2$$

Looking at the decimal part:

How do I calculate this?

My other question is can this answered be stored exactly on a computer?

• How many $1/2$s in $0.1$? How many $1/4$s? How many $1/8$s?... – David G. Stork Sep 17 '18 at 17:56
• @DavidG.Stork would it be $(0.1)_{10} = 2^{-1} + 0.5 =2^{-1} +2^{-1} = 2\cdot 2 ^{-1} = (0.2)_2$ – user123 Sep 17 '18 at 18:00
• No. To make $0.1$ you should add $0$ $1/2$s. (Do you see that?) . You should add $0$ $1/4$s (Do you see that?) . You should add $0$ $1/8$s. (Do you see that?) . and you should a $1$ $1/16$s. (Do you see that?) . Continue with each successive power of $1/2$. If the sequence continues forever, then you cannot represent this number in a computer. (Do you see that?) – David G. Stork Sep 17 '18 at 18:02
• thanks! I believe the answer below gives details of your explanation – user123 Sep 17 '18 at 18:06

## 3 Answers

Note that $0.1=\frac1{10}=\frac12\times\frac15$. Now,\begin{align}\frac15&=\frac3{15}\\&=\frac3{2^4-1}\\&=\frac{3\times2^{-4}}{1-2^{-4}}\\&=3\times2^{-4}\times(1+2^{-4}+2^{-8}+2^{-12}+\cdots)\\&=3\times(2^{-4}+2^{-8}+2^{-12}+2^{-16}+\cdots). \end{align}So, since $3_{10}=11_2$, the binary representation of $\frac15$ is$$0.001100110011\ldots$$and therefore the binary representation of $\frac1{10}$ is$$0.0001100110011\ldots$$Finally, the binary representaion of $5.1$ is$$101.0001100110011\ldots$$This cannot be stored exactly in a binary computer, since it has infinitely many digits.

• Just because it has infinitely many digits doesn't mean it can't be stored in a computer. The expansion is periodic. – Jakobian Sep 17 '18 at 18:05
• @Rumpelstiltskin Indeed. But I suppose that, in this context, what I wrote is what the person who asked the question has in mind. Let us see what the OP has to say about this. – José Carlos Santos Sep 17 '18 at 18:07
• @Rumpelstiltskin since it is periodic it cant be stored? – user123 Sep 17 '18 at 18:07
• exactly stored I mean – user123 Sep 17 '18 at 18:08
• For instance $1/3$ can be stored exactly, but $0.3333333....$ in decimal representation cannot, because it would require an infinitely large memory. (I suspect this is what the questioner's questioner had in mind.) – David G. Stork Sep 17 '18 at 18:11

Numerically you can convert $0.1$ by repeatedly multiplying the fractional part by $2$

0.1 * 2 = 0.2    ->  1. binary digit = 0
0.2 * 2 = 0.4    ->  2. binary digit = 0
0.4 * 2 = 0.8    ->  3. binary digit = 0
0.8 * 2 = 1.6    ->  4. binary digit = 1
0.6 * 2 = 1.2    ->  5. binary digit = 1
0.2 * 2 = 0.4    ->  6. binary digit = 0
....


Now it is periodic and you get $$0.1_{10} = 0.0001100110011001100110011001100110011001100110011\cdots_{2}\\ = 0.0\overline{0011}_{2}$$

Since it is periodic it cannot be exactly stored as binary floating point.

• okay so it can not be stored exactly in a computer then – user123 Sep 17 '18 at 18:09
• thanks for your answer btw! – user123 Sep 17 '18 at 18:09
• There are many ways to 'store' a number, it cannot be stored exactly as binary floating point number (which is mostly used), but it can be stored exactly as base-10 floating point. – gammatester Sep 17 '18 at 18:11

I’d do it this way:

$$0.1=\frac1{10}=\frac12\cdot\frac15$$

Now $$1/5$$ in base $$2$$ is the result of the division $$1_2 \div 101_2$$, so you can set up a long division and divide $$101_2$$ into $$1.000\ldots_2$$. It will be periodic, with period not exceeding $$4$$.

Finally, multiplying by $$\frac12$$ is simply a digit shift (or should I say, a bit shift).