Now we investigate how we can convert a number in base 10 representation to base 2 representation. From the previous section it is evident that when we write a number in base 2 representation we write it as the sum of powers of 2.
Let \(a\in\N\text{.}\) Finding the base \(2\) expansion of \(a\) means finding integers \(r_0\text{,}\)\(r_1\text{,}\)\(r_2\text{,}\)\(\ldots\text{,}\)\(r_{m-1}\text{,}\) and \(r_m\) that are either \(0\) or \(1\) such that
We now continue this procedure with \(q_0=a\fdiv 2\text{.}\) Let \(r_1=q_0\fmod 2\) be the remainder of the division of \(q_0\) by \(2\) and \(q_1=q_0\fdiv 2\text{.}\) Then \(q_0=q_1\cdot 2+r_1\text{.}\) Replacing \(q_0\) by \(q_0=q_1\cdot 2+r_1\) in \(a=q_0\cdot 2+r_0\) we get
Continuing in this way, we successively compute the digits \(r_2\text{,}\)\(r_3\) and so on of the base \(b\) expansion of \(a\) using division with remainder. The quotient becomes smaller in every step. As the quotient is a non-negative integer, it eventually has to become \(0\text{.}\) In this case all subsequent quotients and remainders will also be \(0\) and thus we are done with the conversion.
Example11.11.Conversion of \(13\) to binary representation..
We find the base \(2\) representation of \(a=13\text{.}\) Instead of writing \(q_0\text{,}\)\(q_1\text{,}\) and so on we reuse the variable \(a\) So that when we add a new row the number in the \(a\) column is the number in the \(a \fdiv 2\) column from the previous row. We stop when the number in the \(a \fdiv 2\) column becomes \(0\text{.}\)
Although we computed the base \(2\) digits in this order, it is customary to write the last significant digit, that is the digit with the lowest power of \(2\) last:
As in base \(2\) we only use the symbols \(0\) and \(1\) the base \(2\) representation of a number can be easily converted to a representation of the number by the sum of powers of \(2\text{,}\) namely by leaving out the powers of \(2\) that are multiplied by \(0\text{.}\)
The base 2 expansion of 13 is \(13=1 \cdot 2^3+1 \cdot 2^2 + 0 \cdot 2+1 \cdot 1\text{.}\) Thus the base \(2\) representation of \(13\) is \(1101_2\text{.}\)
[Although writing leading zeros is mathematically correct, it will be marked as wrong. Do not put extra zeros in front of your answer. For example, you should write 101 but not 0101.