CS 100 (Learn)CS 100 (Web)Module 01


Converting to Binary

(direct YouTube link)

NOTE: If your internet access is restricted and you do not have access to YouTube, we have provided alternate video links.

TRANSCRIPT

In this video we are going to learn how to convert numbers in decimal (or base 10) to binary (or base two).

Before we begin, we will want to have our powers of two table handy. For reasons that will be clear soon, we will write them horizontally.

20 = 1
21 = 2
22 = 4
23 = 8
24 = 16
25 = 32
26 = 64
27 = 128
28 = 256
29 = 512
210 = 1024

This table will be provided on exams.

To show you how to do the conversions, we're going to cheat and look at some answers first. Once we have a better intuition for how the conversion works, we will describe a process that will work every time.

The number twenty six base ten [2610] in binary is one one zero one zero [11010‬2].

Let's verify this by using our process to convert from binary to decimal.

We can ignore the zero digits in our number, so looking at where the ones are, we see that the number is two plus eight plus sixteen which equals twenty-six.

What if we had presented this problem as a puzzle?

Looking at our powers of two, we want to find any combination of the powers of two that add up to twenty six. How many combinations can we find?

If you want, you can pause the video and try to find them yourself. I'll countdown from five, four, three, two, one.

It turns out there is only one that combination works and this will always be the case: there will always be exactly one valid combination.

For twenty-six, the combination is sixteen plus eight plus two. Once we have identified the correct columns, we can put a one in those columns, and a zero in all of the others.

    |     |     |   1 |   1 |     |   1 |     |
-----------------------------------------------
128 |  64 |  32 |  16 |   8 |   4 |   2 |   1 |

We have our answer: twenty-six is one one zero one zero in binary [11010‬2].

Let's try another example: can you find the combination of the powers of two that add up to seventy five? Again, you might want to pause the video and try to find them yourself: five, four, three, two, one.

The combination that adds up to seventy-five is: sixty-four plus eight plus two plus one.

    |   1 |     |     |   1 |     |   1 |   1 |
-----------------------------------------------
128 |  64 |  32 |  16 |   8 |   4 |   2 |   1 |

If we add in the zeros, we can see that seventy-five base ten [7510] is equal to one zero zero one zero one one base two [10010112].

So we now need a procedure to be able to do this every time:

Let's see how this works with one more example: eighty-nine. The largest power of two less than or equal to eighty nine is sixty-four, so we put a one in that column.

    |   1 |     |     |     |     |     |     |
-----------------------------------------------
128 |  64 |  32 |  16 |   8 |   4 |   2 |   1 |

We then subtract sixty-four from eighty-nine to obtain twenty-five. We then start over.

The largest power of two less than or equal to twenty-five, which is sixteen.

    |   1 |     |   1 |     |     |     |     |
-----------------------------------------------
128 |  64 |  32 |  16 |   8 |   4 |   2 |   1 |

We then subtract sixteen from twenty-five to obtain nine.

The largest power of two less than or equal to nine is eight.

    |   1 |     |   1 |   1 |     |     |     |
-----------------------------------------------
128 |  64 |  32 |  16 |   8 |   4 |   2 |   1 |

Subtracting eight from nine gives us one, and the smallest power of two less than or equal to one is simply one.

    |   1 |     |   1 |   1 |     |     |   1 |
-----------------------------------------------
128 |  64 |  32 |  16 |   8 |   4 |   2 |   1 |

We can now fill in any missing zeros and we obtain our answer: eighty nine base ten [8910] is equal to one zero one one zero zero one in base two [10110012].

It is easy to come up with your own practice problems. Simply choose a number, convert it to binary, and then convert it back to decimal and check that you got the same number that you started with.