If you don’t get this classic joke, by the
end of this episode you should. Today I explain how to convert binary to decimal, and back
again, here at the House of Hacks. Hi Makers, Builders and Do-It-Yourselfers.
Harley here. In the last two episode of Bits of Binary,
I introduced alternate number systems in general and the binary system in particular. Next
I showed how you can use binary to count much higher than ten on just your fingers. I closed
with the question “How high can you count in binary on both hands?” If you came up with
the answer 1023, you really understand the basics. In this episode in the series, I’m going to
show how to convert from binary to decimal and from decimal to binary. Last time, I explained how each column in
a number is the base number raised to a power times the value of the column. That sounds
more complicated than it is. In our familiar base 10, or decimal, system, the columns are
10^0, 10^1, 10^2 and so forth. This gives us columns that represent units, tens, hundreds
and so on. To get a specific number, say 123, you simply multiply the number in the column
by the column’s value. Or (100 * 1) + (10 * 2) + (1 * 3). Applying this principal to
binary, the columns are 2^0, 2^1, 2^2 and so forth. Giving us 1, 2, 4, 8 and on up. So, let’s convert from binary to decimal.
What’s the decimal value of the number 10101? Given that each column represents a power
of two and each column can only have a value of 0 or 1, this means its value is (2^4 * 1)
+ (2^3 * 0) + (2^2 * 1) + (2^1 * 0) + (2^0 * 1). Removing the items multiplied by zero
gives us (2^4 * 1) + (2^2 * 1) + (2^0 * 1). Evaluating the exponents gives us (16 * 1)
+ (4 * 1) + (1 * 1). And all this simplifies to 16 + 4 + 1 or 21 in decimal. Now that we know the theory, let’s look at
some shortcuts. Instead of looking at the columns as 2 to a power, we can look at them
with specific values. Starting with the units column, we know it’s one. Each subsequent
column is the current column times 2. This gives us 1, 1 * 2 is 2, 2 * 2 is 4, 4 * 2
is 8, 8 * 2 is 16 and so on. Next, all we need to do is write the binary number below
the numbers: 10101. And then simply add the values of the columns with 1’s in them. 16
+ 4 + 1=21 decimal. Binary to decimal is really pretty simple. Next, let’s convert from decimal to binary.
This is slightly more complicated, but still not hard. We need to start with a binary column value
larger than our decimal number. So, we start at the right side with one and multiply by
two until we have a number larger than what we want to convert. Then working from the
left we apply this rule: if the value we want to convert is greater than or equal to the
column value, then we set a one for that column and subtract the column’s value, otherwise,
we set a 0 for that column and continue. The result is then applied to the next column.
And we apply the rule until we reach zero. Let’s try the earlier example of 21 decimal.
First, find the columns. Start with 1 and double until we have a value greater than
21. Then start from the left and apply the rule. 21 is less than 32 so we write a 0 and
move to the next column. 21 is greater than 16, so we write a one below the 16 and subtract
16 from 21 leaving us 5. Next column. Five is less than 8 so we write a zero below 8
and move on. Five is greater than 4 so we set a one below the four and subtract four
from 5 leaving us 1. One is less than 2 so we set a zero below the 2 and move to the
units. One is equal to one so we set a 1 in the units column, subtract 1 from one leaving
us zero and we’re done. If we get to the end without reaching zero,
we’ve done something wrong and need to recheck our work. Looking at the binary value, we have 10101,
which is what we saw in the previous example of binary to decimal, so it all works. Let’s try 24 decimal as another example. 24
is greater than 16, so we set a one below the 16, subtract 16 from 24 leaving us 8.
8 is equal to 8 so set a one below the 8 and subtract 8, leaving us 0. Since we know 0
is less than all the other columns, we can just set them to 0 and be done. This leaves
us 11000 binary. That’s all there is to convert between binary
and decimal. I’ve created a playlist over here that contains
all the episodes in this series so far and will be filled in as more are added. Thanks for watching and if you learned something,
I’d appreciate a thumbs up If you have any questions or comments, leave them below. I
try to respond to all of them. So until next time, go make something. It
doesn’t have to be perfect, just have fun!