Decimal to binary conversion

Data representation

What you should know:

In the lesson on binary numbers we explained that the rightmost bit of a even number in binary is always 0, while in an odd number it equals 1. Moreover, we said that divisions by two can be performed removing the rightmost bit and shifting all the others to the right by one position.

These properties of binary numbers can be used to convert a decimal number to binary: the rightmost bit can be calculated just checking if the number is even or odd. To discover the other bits we just perform several divisions by two, rounding the result to the lowest value. In this way, we can generate decimal numbers whose binary counterpart contains all the bits of the sequence we have to discover, except the rightmost one. This means that, with every division, the second bit in the sequence becomes the first one can be easily evaluated just checking if the result of the last calculation is even or odd.

For instance, if we have to convert the decimal number 74, we already know that the rightmost bit is 0, because the number is even.

Then we divide 74 by two, which yields 37. The result is odd, meaning that the second bit in the sequence equals 1.

The complete calculation can be performed as shown in the following table.

NumberConditionRight bitBinary

The result can be written from left to right, taking the bits starting from the bottom of the table.