Learn Interactively: Binary Code

To understand how binary code works, you can look at its big brother first; The Decimal System.

We defined ten specific symbols to denote values.

9 8 7 6 5 4 3 2 1 0
You know this, easy enough.


But now think about how counting works. I hear you instinctively mumble 1, 2, 3, .. inside your head but let's define that a little more verbosely.

By "counting up" we mean incrementing a value by one unit.

0

9

By "counting down" we mean decrementing a value by one unit.

Alright, we can count now.

Exciting, I know!

That's great and all, but that's not enough. We need a mechanic for dealing with values higher than our highest symbol, because inventing new symbols for every single value is not practical.

From experience, you already know how to count into the two digit range but what exactly happens there? Again, let's be a little verbose and generic.

When you reach the highest possible symbol, wrap back around to the lowest symbol and increment the digit to your left.

0


7


> Go ahead and press the +1 button. Text is gonna get generated here.

I know that you know this, but really think about how the number grows, step by step.

I specifically tried to word this whole process as general as possible, so we can reuse it later when counting in binary. Before we do that however, let's take a smaller step.

Let's push the first boundary of our mind. Take a mental snapshot. 

What would change if we'd remove the symbol '9' from our currently used numeral system?

Without '9' we are left with:

8 7 6 5 4 3 2 1 0
Easy enough, right?

Remember how we previously counted, let's do it again. 

0


6


> Go ahead and press the +1 button. Text is gonna get generated here.

You should feel weird now, taken you've never done this before. If you grasp this concept however, you can count in pretty much any numeral system (including binary)!



Let's translate some numbers from this nonary (9 digits) numeral system to the more familiar decimal system.

Nonary
Decimal

0


0

Compare how the numbers grow. Hammer this point home:
When you reach the highest possible symbol, wrap back around to the lowest symbol and increment the digit to your left. See how this process works exactly the same for both systems.

We managed to reduce the number of possible symbols from ten to nine. It's time to reduce them even more, you're here to learn binary after all. 

Go through the next examples thoughtfully and try to think of how the numbers will look - before - you change the value. 

Symbols Name Value
5 Quinary 0
4 Quaternary 0
3 Ternary 0



10 Decimal
0


By now, you should know why the numbers look the way they do. We're only one digit away from binary, are you ready?

Remember, this process works exactly the same way in binary; You just reach the highest symbol very quickly, so you need to wrap around really quickly as well.

Let's count the first three digits together, step by step.

Decimal
Binary

000

000

We increment the most right digit by one unit, until we reach the highest symbol.

Practice: Small Numbers

Here are some numbers which you should try to translate, to check if you understood the material. Use anything (your mind, your fingers, paper or even the textbox itself) to keep track of the numbers.

Quick note: No leading zeros are needed!

Decimal
Binary

1


How to easily convert binary to decimal in your head

You've seen how counting works and the general concept behind binary, but it's probably still a bit foreign because counting to, for example, 22 is tedious and unintuitive.

Luckily, there's a really convenient property of the binary numeral system which you can use in your head!

Let's stick to the chosen example of the decimal number 22.

Create a sequence, or rather, table of numbers that equate to powers of two. It's considerably less scary than it sounds once you see it visually. Most people can deduce the missing number in the following sequence:






? 8 4 2 1

So far so good, now to the good part. 

When you put a binary table beneath said sequence, you can simply add up all the numbers with a 1 underneath them.

Let's say that we have decimal numbers up to 16 i.e.

16 8 4 2 1

and we want to translate the following binary number: 10110
This results in the following table:

Decimal




16 8 4 2 1 22






1 0 1 1 0
Binary




Another example. This time 1011:

Decimal




16 8 4 2 1 11






0 1 0 1 1
Binary




Here's an interactive table to see it in action.

Decimal




16 8 4 2 1 0






0 0 0 0 0
Binary







Process: Head conversion

The thought process when trying to translate a decimal number to binary is easiest performed from left to right. Let's, say that we want to find the binary number for '19'.

Start with the largest power-of-two-number that will fit in.

32? No, too big.
The next smaller one; 16? Yes, a match!
Put a '1' down and construct the table, left to right.

Decimal




16 8 4 2 1 16






1
Binary




Your goal now is to find the numbers that add up to '19'. There a few ways, my personal thought process works like this:

Now (19-16) equals 3, so we try to match 3 going forward.

The next smaller one; Does 8 fit into 3? No, 8 is bigger than 3.
Put a '0' down.

Decimal




16 8 4 2 1 16






1 0
Binary




The next smaller one; Does 4 fit into 3? No, is bigger than 3.
Put a '0' down.

Decimal




16 8 4 2 1 16






1 0 0
Binary




The next smaller one; Does 2 fit into 3? Yes! 2 is smaller than 3.
Going forward we're trying to match 1 now, because (3-2) equals 1.
Put a '1' down.

Decimal




16 8 4 2 1 18






1 0 0 1
Binary




The next smaller one; Does 1 fit into 1? Yes (of course), we're done.
Put a '1' down.

Decimal




16 8 4 2 1 19






1 0 0 1 1
Binary




Do this a couple of times and it will become easier.

Equipped with your new knowledge and some practice you should be able to decipher larger binary numbers! Here's a similar exercise like before, but with bigger numbers:

Quick note: No leading zeros are needed!

Decimal
Binary

1


Afterword

Thank you very much for reading!

If you're not quite getting it, please let me know where you got stuck.

My main goal with this post is helping you understand how our numeral systems work. If you understood the material, you should be able to count in other bases as well. For example, I mentioned Ternary (base 3), which has real world use cases like expressing the state of CMOS circuits (and more).

There's also hexadecimal (base 16) which is very widely used in computers for displaying many different kinds of values, including color referencesIP adresses, text, etc.

Please let me know if you enjoyed this article, I'm always looking for feedback to improve upon.

What did you like about this post? Got questions? Get in touch!

( no registration required! )

2 Kommentare

  • this was fantastic, thanks so much for putting this resource together. it really helped with wrapping my head around it in a simple way, and more importantly, know how to teach it to others.
  • Hey there, thank you so, so much for the kind words! Really glad you enjoyed it!

Was denkst du?

Side note: Comments will appear after being spam filtered, this might take a moment.

Contact me

LinkedIn
© Daniel Biegler
Design & implementation by
Daniel Biegler