A mathematical function for denary to binary conversion.

I an looking for the mathematical notation for a function that converts base 10 to base 2. The domain for the function should ideally be $\mathbb Z^+$, but a domain for one digit numbers would also be alright. I have tried using logarithms but I cannot understand how to create the binary string. Any help would be greatly appreciated.


First of all base 10 and base 2 are notations and not the integers themselves. And integer will have a value no matter how it is written. So if you want a function that converts base 10 to base two (a function being a mapping between two sets) then this is not a function on the integers but a function on strings of digits to another string of digits.

If you want a function from the integers themselves to a binary string (then it doesn't actually matter what notation the integer is notated in) there is no real formula for this but there are two recursive methods of doing this.

Method 1: Big to little.

Let $N$ but the natural number. Let $n=\lfloor \log_2 N \rfloor$ (where $\lfloor . \rfloor$ is the greatest integer less than or equal function). Then the string will have $n+1$ terms and the "leftmost/largest" term is $a_n = 1$. Let $N_{n}= N$.

Recursively for any $N_{m}$ and $a_m$ you have defined, define $N_{m-1}= N_m - a_m\cdot 2^m$. If $N_{m-1}\ge 2^{m-1}$ define $a_{m-1}:=1$. Otherwise define $a_{m-1}:=0$.

Repeat until you have figured out $a_0$.

For example to conver $364_{10}$ to binary.

As $2^8=256 < 364 < 2^9$ we know $8<\log_2 364 < 9$ so $n=\lfloor \log_2 364 \rfloor = 8$. $N_8 = 364$ and $a_8 = 1$.

$N_7 = 364 - 2^8 =364-256 = 108$. $108 < 128=2^7$ so $a_7=0$.

$N_6 = 108 - 0\cdot 2^7 = 108$. $108 > 64 = 2^6$ so $a_6 = 1$.

$N_5 = 108 - 1\cdot 2^6=104-64=44$. $44 > 32 = 2^5$ so $a_5 = 1$.

$N_4=40-1\cdot 2^5 = 40-32 = 12$. $12 < 16 = 2^4$ so $a_4 = 0$.

$N_3 = 12 -0\cdot 2^4=12$. $12 > 8 = 2^3$ so $a_3= 1$.

$N_2 = 12 - 1\cdot 2^3 =12 -8 =4$. $4 \ge 4=2^2$ so $a_2 = 1$.

$N_1 = 4-1\cdot 2^2 =4-4=0$. $0 < 2=2^1$ so $a_1 = 0$.

$N_0 = 0-0\cdot 2^1=0$. $0 < 1 = 2^0$ so $a_0 = 0$.

So in binary $364_{10} = 101101100_2$.

Method 2: Little to big

Let $N = N_0$ be the natural number. Let $a_0$ be the remainder of $N_0$ when divided by $2$.

For any known $N_m$ and $a_m$ let $N_{m+1} = \frac {N_m - a_m}2$. And let $a_{m+1}$ be the remainder of $N_{m+1}$ divided by $2$. Repeat until you come to a point where $N_m = 0$.

Example. To convert $364_{10}$ to binary.

$N_0 = 364$. The remainder when we divide by $2$ is $0$. So $a_0 = 0$.

$N_1 = \frac {364-0}2 = 182$. The remainder when we divide by $2$ is $0$. So $a_1 = 0$.

$N_2 = \frac {182-0}2 = 91$. The remainder when we divide by $2$ is $1$. So $a_2 = 1$.

$N_3 =\frac {91-1}2=45$. The remainder when we divide by $2$ is $1$. So $a_3 = 1$.

$N_4 = \frac {45-1}2= 22$. The remainder when we divide by $2$ is $0$. So $a_4 = 0$.

$N_5 = \frac {22-0}2 =11$. The remainder when we divide by $2$ is $1$. So $a_5=1$.

$N_6 = \frac {11-1}2 =5$. The remainder when we divide by $2$ is $1$. So $a_6=1$.

$N_7 = \frac {5-1}2=2$. The remainder when we divide by $2$ is $0$. So $a_7=0$.

$N_8 = \frac {2-0}2 = 1$. The remainder when we divide by $2$ is $1$. So $a_8=1$.

$N_9= \frac {1-1}2 = 0$. We are done.

So in binary $364_{10} = 101101100_2$.

=====

A third method (which can be uncoil and poke you in the eye if you are not careful) is to note that $TEN = 2^3 + 2 = 1000_2 + 10_2$ and to know the single digit conversion.

Watch

$364_{10}= 3_{10}\times TEN^2 + 6_{10}\times TEN + 4_{10}=$

$3_{10}\times(1000_2 + 10_2)^2 + 6_{10}\times (1000_2+ 10_2) + 100_2=$

$(3_{10}\times 1000_2 + 3_{10}\times 10_2)(1000_2+10_2) + 110_2(1000_2 + 10_2) + 100_2=$

$(11,000 + 110)(1000+10) + 110,000 + 1,100 + 100 =$

$11,000,000 +110,000 + 110,000 + 1,100 + 110,000 + 1,100 + 100 =$

$11,000,000 + 110,000 + 110,000 + 1,000 + 110,000 + 1,000 + 100+100 + 100=$

$11,000,000 + 110,000 + 110,000 + 1,000 + 110,000 + 1,000 + 1,100=$

$11,000,000 + 110,000 + 110,000 + 110,000 + 1,000 + 1,000 + 1,000 + 100=$

$11,000,000 + 110,000 + 110,000 + 110,000 + 11,000 + 100=$

$11,000,000 + 110,000 + 110,000 + 110,000 + 10,000 + 1,100=$

$11,000,000 + 100,000 + 100,000 + 100,000 + 10,000 + 10,000 + 10,000 + 100,000 + 1,100=$

$11,000,000 + 1,100,000 + 1,000,000 + 1,100=$

$11,000,000 + 1,000,000 + 1,000,000 + 100,000 + 1,100=$

$11,000,000 + 10,000,000 + 101,100=$

$10,000,000 + 10,000,000 + 1,000,000 + 101,100=$

$100,000,000 + 1,101,100=$

$101,101,100_2$


The mathematical function you seek does not have a special name, nor does it have a formula. If you search for decimal2binary you will find algorithms for pen and paper calculation and coded in many languages.

You can give that function any name you like.

For single digits just use a table:

0    0
1    1
2   10
3   11
...
9 1001