Converting from binary to decimal can get tedious, especially as our binary data grows in length. Lucky for us, converting from decimal to binary is very straightforward.
The method we will use only requires us to be able to divide by
2, hence its name, Division-by-2 Method.
Whenever we divide a number we always have two answers:
- the result (also known as quotient)
- the remainder
If our remainder is
0, we normally don’t include it when we give the answer but it is still there.
35 / 7 = 5 remainder 0
36 / 7 = 5 remainder 1
When we divide by
2 we will always end up with a remainder that is either a
1 or a
0, in other words, binary digits.
The Divide-By-2 method will continue dividing a number by two until the result, the first part of the answer, is
0. The first time we divide, the remainder goes in the LSB column. Each time we divide after that, the remainder is written as the next digit to the left.
We can use our even/odd trick we learned to check if we’ve got the right digit in the LSB. Odds will always be
1‘s and evens will be
Follow along with the conversion of 2710 to binary:
|27 (odd)||2||13||1 -> LSB||1|
|1 (odd)||2||0||1 -> MSB||11011|
We are going to be converting the number
20610 from decimal to binary.
Create two variables,
first_remainder_lsb, and set them equal to the first stage of the Division-by-2 Method conversion.
Create the variables
final_remainder_msb. Continue the conversion and set each variable equal to the appropriate answer.
Now that we’ve completed our Division-by-2, create the variable
final_binary_number and set it equal to the complete result of your Division-By-2.