Number Base Conversion

Algorithm
This is an algorithm for converting natural numbers to binary form.

Let $n \in \N$ be the number to be converted.

Let $x \bmod 2$ be the modular operation $2$ on $x$, that is, the remainder of $x$ on division by $2$.

Let $\left[{b_m b_{m-1} \ldots b_2 b_1 b_0}\right]_2$ be the base $2$ representation of $n$.


 * Step 1:

Take $d_0 \bmod 2$. Call this number $a_1$

If the remainder is $0$, then $b_0$ is $0$. Take $\dfrac {d_0} 2$ and go to step $2$. Call this number $a_1$.

If the remainder is $1$, then $b_0$ is $1$. Take $\dfrac {d_0} {2} - \dfrac 1 2$ and go to step $2$. Call this number $a_1$.


 * Step 2:

If $a_1 = 0$, stop.

If $a_1 \ne 0$, then take $a_1 \bmod 2$.

If the remainder is $0$, then $b_1$ is $0$. Take $\dfrac {a_0} 2$ and go to step $3$. Call this number $a_2$.

If the remainder is $1$, then $b_1$ is $1$. Take $\dfrac {a_0} {2} - \dfrac 1 2$ and go to step $3$. Call this number $a_2$.


 * $\vdots$

Continue this process until Step $m+1$ gives $a_{m+1} = 0$, then stop.