Conversion from the Decimal system to the Natural Binary Code


The conversion of a decimal number to the binary format is very simple. The decimal value is simply divided by 2 and then the remainder is written down, the process is repeated until the number cannot be divided by 2 anymore. The next step requires rewriting the remainders in the reverse order and by this obtaining the result in binary code.

Example:
157

157 | 2 1 (LSB)
78 | 2 0
39 | 2 1
19 | 2 1
9 | 2 1
4 | 2 0
2 | 2 0
1 | 2 1 (MSB)

10011101 = 157

In the natural binary code it is also possible to convert fractions of a decimal number. Conversion algorithm in this case differs from the one performed on integers. Instead of division, multiplication of the fraction by 2 is performed. The process repeats until the result is equal to zero.

Example:
0.625

0.625* 2 = 1.25 1
0.25 *2 =0.5 0
0.5 * 2 = 1 1

The binary result is produced by rewriting the remainders in order.
0.625=0.101