Convert from base 10 to base 2 and back again

code

We are going to document a formula for converting from base 10 to base 2 and back again. We would use 35 as the base 10 value.

 

To convert 35 to base 2, we would divide 35 by 2 and keep the remainders. eg.

 

35 / 2  = 17 r 1
17 / 2  = 8  r 1
8  / 2  = 4  r 0
4  / 2  = 2  r 0
2  / 2  = 1  r 0
1  / 2  = 0  r 1

 

The base 2 value of 35 is 100011.

 

To convert 100011 to its base 10 value which is 35, we would multiply each base 2 digit by 2 and the power of their position. e.g.

 

1 * 2^0 = 1
1 * 2^1 = 2
0 * 2^2 = 0
0 * 2^3 = 0
0 * 2^4 = 0
1 * 2^5 = 32


Then we would add all the resulting values together.

 

1+2+0+0+0+32 = 35

 

Hope this helps you simplify the conversion process.