Learn

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 0‘s.

Follow along with the conversion of 2710 to binary:

Dividend10 Divisor10 Result10 Remainder10 Cumulative Binary2
27 (odd) 2 13 1 -> LSB 1
13 (odd) 2 6 1 11
6 (even) 2 3 0 011
3 (odd) 2 1 1 1011
1 (odd) 2 0 1 -> MSB 11011

**Answer: 2710 = 110112

Instructions

1.

We are going to be converting the number 20610 from decimal to binary.

Create two variables, first_result and first_remainder_lsb, and set them equal to the first stage of the Division-by-2 Method conversion.

2.

Create the variables third_result, fourth_remainder, sixth_result, and final_remainder_msb. Continue the conversion and set each variable equal to the appropriate answer.

3.

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.

4.

Finally, uncomment the last few lines of code and view the results in the console. As you can see, most programming languages have built-in methods to do these conversions for us, here is Python’s documentation on bin() and int().

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?