[Math] Why does the method of converting from decimal binary by taking remainder work

arithmeticbinary

For example, you want to 100 decimal to a binary number in zeroes and ones. You keep divide 100 by 2 until you have 0 left. And the remainders will be a binary number. 1100100. The first 1 is the remainder you get by dividing 1 by 2. The last 0 is the remainder you get when you first divide 100 by 2.

The easiest-to-understand way of converting from decimal to binary is, first find the largest power of 2 that is smaller than 100. And substract that power of 2 from 100. And find the largest power of two that is smaller than that difference we found. Repeat this over and over until you get to 1, which is the two to the zero power.

Best Answer

The binary expression of the number $n$ is given by $$ n=a_0+a_1\cdot2+a_2\cdot 2^2+\dots+a_k\cdot 2^k $$ where $a_i$ is $0$ or $1$ and $2^k$ is the largest power of $2$ less than or equal to $n$ (and $a_k=1$).

When you divide $n$ by $2$, the remainder is $a_0$ and the quotient is $$ n_1=a_1+a_2\cdot2+\dots+a_k\cdot 2^{k-1} $$ When you divide $n_1$ by $2$, the remainder is $a_1$ and the quotient is $$ n_2=a_2+\dots+a_k\cdot 2^{k-2} $$ Continuing in this way, we arrive finally at $$ n_k=a_k $$ Thus the procedure of successively dividing by $2$ and collecting the remainders produces the digits in the binary representation of $n$, starting from the less significant digit. Note that we don't need to know $k$ in advance: the algoritm stops when the last quotient is $0$.

Finding the largest power of $2$ less than or equal to $n$ yields the most significant digit. It's an alternative procedure, but requires to know the powers of $2$, which is not needed with the algorithm of successive divisions.

So successive divisions provide a more efficient algorithm.

By the way, the algorithm is the same for every radix. The alternative method for radix $b$ requires a table of the powers of $b$ together with their multiples by $2$, $3$ and so on up to $b-1$.