Skip to content

Instantly share code, notes, and snippets.

@abstractmachines
Last active September 20, 2020 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abstractmachines/6d0c2b9f1eb79cfa9904d07fe101637f to your computer and use it in GitHub Desktop.
Save abstractmachines/6d0c2b9f1eb79cfa9904d07fe101637f to your computer and use it in GitHub Desktop.
Intro To Binary

WIP. This is a tutorial I'm writing which is in progress :) - Amanda

Intro to binary (and some computer architecture)

Intended audience(s) include:

  • Very early career engineers who don't have a 'math background' (yet!)
  • Junior developers and interns who want to "understand" rather than just "complete tutorials"
  • Web developers who didn't take CS courses, but still want to understand "how things work" "under the hood"

Definition of done/success:

Contents

All Languages Become Binary

Eventually, all computer languages are translated, by your computer, down to binary language. It is the only language your computer actually speaks! JavaScript, Java, C++, you name it, it all becomes binary eventually! This happens through advanced translating systems that have to do with languages and "syntax" and "parsing." We aren't focusing on the details of those things right now.

What is binary code?

What is binary code? Well, we know it means "two" of something, don't we? Binary means two. This means that anything that is "in binary" must either be a one, or a zero. Here's what binary looks like: 0010 1111 1101 0010

Binary is not only a number system (see base below in references), but also a computer programming "language" - just like "JavaScript" or "Python" or "C."

Radix

"Radix" just means "how many kinds of different things you have in a counting system." There are certainly more complicated descriptions, but this is a tutorial for beginners.

  • How many "things" does decimal have? There are ten numbers, right? 0 through 9. That's ten things! So the Radix, or Base, is 10. So we call decimal "base 10." This means that as you move right to left and read every digit, every placeholder (for the next digit) will multiply by ten, so that's why it goes "1 ... 10 ... 100 .... 1000 ..." but read in the opposite direction, from right to left. If this confuses you, look at the table under the counting section.

  • How many "things" does binary have? 1 and 0. That's two things! So the Radix, or Base, is 2. So we call binary "base 2." This means that as you move right to left and read every digit, every placeholder (for the next digit) will multiply by two, so that's why it goes "1 ... 2 ... 4 .... 8 ..." but read in the opposite direction, from right to left. We will cover more on this below.

Know what's cool? There are other bases too. But we don't care about that right now. We want to learn only about binary!

As a final note on reading "right to left," we usually use the acronym RTL.

To understand counting in binary, we must review counting in decimal

Have you ever counted to 10? Or 20? or 552? You're using the decimal number system when you do that.

There's a Wikipedia article which describes the "Decimal Numbering System." I'm not going to link it here because it can be a bit confusing and intimidating to read about these kind of things when you haven't begun to "understand" or "grok" what is happening. We'll do that later in the references section below! For now, let's learn simple stuff.

When you count to 10, it looks like this, doesn't it? It's "left to right."

1 2 3 4 5 6 7 8 9 10

Pretty easy, right? Well, let's think about something different. Let's think about position, and let's read numbers in a more "right to left" way instead.

Position (aka "place"): A Silly Exercise

Think Right To Left

Think about what you learned in school about the "ones place" and "tens place." That's what we're studying here. We will learn to read numbers right to left. You've already used this system for years, but you haven't read right to left. You've read it the other way. Let's reverse how we think of numbers so that understanding "binary" is easier!

Let's start thinking less about the value of numbers (whether it's a 1 or a 600), and more about what position that number is in.

Let's talk about the number 0. We think of the number 1 like this:

1

Very easy, right?

Well, how about this? Is this still 1?

01

Yup, that's still 1. But what does it mean to have 01?

Counting (turn zero into one)

Rather than thinking about "01", think about the position of each individual digit.

  • What position is the number - each individual digit - in?

This number is zero, right?

00

Ok, great. We have zero. Now, how we change that to a 1? Does it matter which zero we change? Yes, of course it does. And that's all because of place, or "position."

Let's do an silly exercise to learn about or refresh our brains on what "position" means in number systems.

Let's make that zero into a one.

We have:
00

... now, let's turn that zero into a one! Is this right?
10

... is that one? Nope. It's ten! Whoops. 

Hmm. Looks like we aren't using the right place (another word for position). We changed it in the tens place rather than the ones place.

How about changing the OTHER zero - the zero that is in the ones place into a 1?

We have:
00

01
... is that 1? Yes!

So the position of the number really does matter, doesn't it?

In the decimal number system, each time we increment (add to) the number in the ones place, the number increases by 1.

01 -> this is 1
02 -> this is 2
03 -> this is 3

Which could also be written as:

00001 -> this is 1
00002 -> this is 2
00003 -> this is 3

Each time we increment (add to) the number in the tens place, the number increases by 10.

10 -> this is 10
20 -> this is 20
30 -> this is 30

Are you seeing the places and positions and how they work now in a way you can "grok"?

010 -> this is 10
011 -> this is 11
020 -> this is 20
120 -> this is 120. Using the hundreds place now!

Decimal system positions: read Right To Left, and note that each "place" is the last one, multiplied by ten!

This is how you, a real nerd, will now think about the number "3" when it's in decimal:

0 0 3
hundreds place tens place ones place

Challenge and Exercises Section


Challenge: convert 003 from decimal to binary

The decimal system number of 3 is also read as 003, which means (when read Right To Left) "three ones, zero tens, and zero hundreds." Convert that decimal number 3, into the binary version of that same number, 3.

Example: This is what "3" looks like in the decimal system.

This is the number 3 in the decimal system.

0 0 3
hundreds place tens place ones place

Exercise: Fill out this template and convert to binary. You can only use a 1 or 0! You cannot use a "2" or 3."

Fill out this chart, using only 0's and 1's, and make it say the number "3", only in binary.

? ? ?
fours place twos place ones place

** Stop and ask yourself if you can change those question mark symbols to a 0 or a 1, to get a total of 3! **


Challenge Solution: The number 3, written in binary, is 011

0 1 1
fours place twos place ones place

Extra Credit Challenge

What binary number is this? 100

Extra Credit Challenge Solution: The number is 4

1 0 0
fours place twos place ones place

Extra Credit Challenge 2

What binary number is this? 1010

Extra Credit Challenge 2 Solution: The number is 10

1 0 1 0
eights place fours place twos place ones place

Computers Speak in Binary

Here's a bit:

0

That bit means OFF (it can also mean other things, which we don't care about right now).

Here's another bit:

1

That bit means ON (it can also mean others things, again, which we aren't focusing on).

  • On and Off. 1 and 0. Bits of information!
  • 1010 kind of means, "on, off, on, off," doesn't it?
  • Have you ever heard of voltage or electrical current? Like, power, right? Cool. Well power can turn on and off, right?
  • Let's think about what this article called "Memory Cell" says:

"The memory cell is the fundamental building block of computer memory. The memory cell is an electronic circuit that stores one bit of binary information and it must be set to store a logic 1 (high voltage level) and reset to store a logic 0 (low voltage level). Its value is maintained/stored until it is changed by the set/reset process. The value in the memory cell can be accessed by reading it."

  • After reading that, you are probably thinking "Maybe I understand part of that, but a lot of it went over my head. I think that maybe I understand how when there is a 1, current and voltage exist/are flowing/stuff is on, and when there's a zero, things are off, and there's no current/voltage happening." And that's not a bad conclusion for now with your learning on why binary matters for Computer Architecture and for "understanding a bit more about how computers work."

References

Note: If you have not completed CS or engineering courses, please don't read this section and then beat yourself up for having no idea what these articles are on about. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment