home
NOTE: Under Construction - I'm in the middle of upgrading my site and lots of stuff is kinda broken. Please forgive the mess.

How Computers Work: The 1s And 0s

April 2024

Introduction

Computers are amazing. From the simplest notes app on your phone to the incredible systems that put robot helicopters on Mars, they power our world. And, most amazingly, they do it all with nothing more than a bunch of 1s and 0s.

This is the story of how that works.

Where It All Starts

A Toggle Switch

Before we talk about the 1s and 0s, let's look at a light switch. It has two positions: OFF and ON. Clicking or tapping the switch flips it back and forth between the two positions.

Try it and see.

a light switch in the off position

By the way, a formal name for that flipping back and forth is "toggle" (e.g. if we toggle the switch when it's in the OFF position it gets turned ON. Toggling it while it's ON turns it OFF). We'll be using that word a lot in this post.

Adding Some Light

A switch that isn't connected to anything isn't a lot of use. Let's connect ours to a light. Now clicking the switch toggles the light on and off too.

a light switch in the off position with a light bulb above it that's not illuminated

Switching Between Things

Of course, our switch doesn't have to control a light. It could control a fan, a garbage disposal, or an air conditioner. Anything that needs to be turned off and on.

Instead of turning something off and on, we could also use the switch to toggle back and forth between two things. For example, we could have the switch control an "Open/Closed" sign for a shop.

Toggling this switch ON shows the "Open" sign. Toggling it OFF changes the sign to "Closed".

a light switch in the off position with a closed sign above it

A Little Bit Of Data

Instead of a sign that says "Open" or "Closed", let's attach a sign with a number on it. When the switch is OFF, the number will be "0". When the switch is On, the number will be "1".

a light switch in the off position with a number zero above it

We'll be using this exact setup for the rest of this post. The 0s and 1s will become little bits of data that we combine with each other to do useful things. So, lets call each switch and its number a "bit".

And, believe it or not, that's all we need to make a computer.

Imaginary Computers

Laying The Groundwork

We'll be making a bunch of imaginary computers as illustrations. Each one will have two parts:

Computers don't have brains. The way they "remember" things is by toggling microscopic bits inside themselves that they can check later when they need to look up information. That's the reason we're calling the area "Memory".

Our First Computer

To get started, here's a computer with a button on the Screen that doesn't do anything and without any bits in the Memory. There's nothing useful about it other than showing us what our illustrations are going to look like.

Our First Bit

Now, let's make a computer that does something. This one has a single bit in its Memory. Since this is an Imaginary Computer, we could click the switch to toggle the bit directly in the Memory even though it's "inside" the computer. But, let's set it up more like a real world computer and add a "Toggle Bit" button on the Screen that we'll use to do the toggling.

Give it a shot to test it out.

Seeing The Bit

We can see our bit in the Memory of our Imaginary Computer, but, since it's "inside" the computer, we wouldn't be able to see it in the real world. So, let's show the current number for the bit beside the button on the Screen as well.

Now, when we click the "Toggle Bit" button we see the displayed value on the Screen change as well as the actual bit in Memory:

A Second Bit

We can store more than one bit in our computer's Memory.

Let's add another one connected to another button and label the bit/button pairs "A" and "B" so we can keep track of what's connected to what.

Clicking one of the buttons toggles the corresponding bit without affecting its neighbor.



A

B

Counting With Bits

Adding Bits Together

Apps can combine the values from multiple bits in Memory together to do useful things on the Screen. For example, we can make a "Total" display on the Screen that shows the values of the bits in Memory added together.

Click the buttons and you'll see that:

0
0

Total    

A

B

Adding Another Bit

If we wanted to count to three, we could add another bit/button pair like this:

0
0
0

Total    

A

B

C

We can now get a Total value of "3" if we toggle all the bits to "1".

We could use this approach to count on our computers, but there are two things about it that aren't great:

Neither of those things is awful, but there's a better way to do things.

Counting With Multipliers

The technique to eliminate both issues is to multiply the value of each bit by a specific amount before counting it.

Specifically:

Try toggling these bits one at a time and watching the Total. Then, do two at a time, then all three.





Total            

A

B

C

The first number beside each button is still the value of the bit itself and matches what's above the corresponding switch in the Memory (i.e. either 0 or 1).

The next number is the multiplier value (e.g. 1, 2, or 4).

The final number at the end of the line for each button is what gets used to calculate the Total. It's either "0" if the bit is toggled to "0", or it matches the value of the multiplier (e.g. 1, 2, or 4) if the bit is toggled to "1".

Using the multipliers does two things:

You can play around with the Screen toggle buttons to get a Total of 0, 1, 2, 3, 4, 5, 6, and 7 and to see there's only one way to produce each value.

Doubling Growth

Let's add two more bit/button pairs.

We'll make a "D" bit with a multiplier of "8" and an "E" bit with a multiplier of "16".

While we're at it, I'm going to reverse the order of the switches in Memory from: "A, B, C, D, E", to: "E, D, C, B, A". That doesn't have any affect on the way things work. It's just to make things easier to read in later examples by putting the bit with the switch with the highest multiplier first.

Play around and you'll see that with five bits we can now count from 0 (with all the bits toggled to 0) to 31 (when all the bits are toggled to "1"). And, we can get any number in between by toggling some bits to "1" and others to "0.







Total            

E

D

C

B

A

We can also start to see the pattern for how the multipliers work. Every time we add a new bit we double the value of the multiplier from its predecessor by multiplying it by 2.

So, we start with:

If we added three more bits, they'd be:

Binary Numbers

Putting the bits into an order with the highest multiplier first has an interesting effect. We can now read all the bits together and treat them like their own number. And, those numbers always correspond exactly to specific Total values.

For example:

Here's a full list of the thiry-two possible values we can produce with the five bits in our prior example.

Memory Total
00000 0
00001 1
00010 2
00011 3
00100 4
00101 5
00110 6
00111 7
Memory Total
01000 8
01001 9
01010 10
01011 11
01100 12
01101 13
01110 14
01111 15
Memory Total
10000 16
10001 17
10010 18
10011 19
10100 20
10101 21
10110 22
10111 23
Memory Total
11000 24
11001 25
11010 26
11011 27
11100 28
11101 29
11110 30
11111 31

The Rules Of Computer Bits

We now have the complete set of rules for how computers work with bits:

They may not seem like much, but we'll see in the rest of this post how those four rules are the foundation of how computers work.

Letter And Number Examples

Moving Up And Down

So far, each button on the Screen has been paired directly with a single bit in Memory. Apps can also use buttons that control multiple bits at the same time.

For example, here's a counter with five bits in Memory but only two buttons. One that adds to the Screen's Total and one that subtracts from it.

A:
B:
C:
D:
E:

Total      


E

D

C

B

A

Something to notice here is that clicking "Subtract" when the Total is "0" or clicking "Add" when the Total is 31 doesn't do anything. That's because our Memory can only deal with 32 values. It's 0 when all the bits are toggled to "0", and it maxes out at 31 when all the bits are toggled to "1".

We'd need more bits in Memory to use more numbers. And, we'd have to come up with a way to deal with negative numbers. We could do that by changing the meaning of one of the bits so that instead of representing a number it represented with "+" or "-" sign of the number.

Converting Bits To Letters

Everything we've done to this point has displayed numbers on the Screen based directly on the value of the bits in Memory We've either displayed them directly or added them together with their multipliers to get a Total number value.

Since the bits in a computer's Memory can only be 1s or 0s we'll have to come up with a new technique to handle letters. We'll do this by creating a "Memory Map Dictionary" which works like this:

For example, we can make a Memory Map Dictionary of letters in the U.S. English alphabet that looks like this:

The computer can look up entries in the Memory Map Dictionary using either the letter or the number. So, it can look up the letter "B" to see that its number is "2". Or, it can go the other way and look up the number "25" to see that its corresponding letter is "Y".

With this Memory Map Dictionary the computer can store number values in bits and translate them into the letter to display on the Screen.

A:
B:
C:
D:
E:

Total      
Letter      


E

D

C

B

A

(Note: five bits gives us the ability to count from 0 to 31. Since there are only 26 letters in the U.S. alphabet, I set the entries in our Memory Map Dictionary for values 27-31 to say "no letter ###".)

More Than Letters

Of course, there are more than just U.S. English alphabet letters involved in writing. Punctuation, accent marks, and languages like Chinese, Japanese, and Korean that use entirely different characters, for example.

For a real-world computer app we'd want to be able to handle them all. Thankfully, a bunch of folks already got together and created a Memory Map Dictionary that includes everything for us. It's called Unicode 1. Here's a basic example that uses it.










Total              
Character          


H

G

F

E

D

C

B

A

(Hint: The Unicode standard includes lots of characters that don't show up under various circumstances. So, you'll see "[invisible]" a lot. The U.S. English "A" is located at "01000001" which you can get to by toggling bits "G" and "A" to 1 and then going from there)

Typing A Letter

So far we've been using buttons to update the values of the bits in Memory, but you wouldn't want to have to click a bunch of buttons every time you want to type something. Instead, you'd want to use a text box like this one where you can type a letter.

A:
B:
C:
D:
E:
F:
G:
H:

Total        


H

G

F

E

D

C

B

A

It's important to point out that the difference here is completely on the Screen side of things. We're interacting with a text box instead of a bunch of buttons, but the bit values the computer uses in Memory are exactly the same as when we set them individually in the prior examples.

If you type a lowercase "a" in the box you'll see "01100001" in the Memory. If you go to the prior example and toggle bits "G", "F, and "A" to "1" you'll see the same thing in Memory and the lowercase "a" listed as the Character on the Screen.

Typing Words

Let's take the step from letters to words. We'll still use bits in Memory, but things are going to look a little different. Our Unicode example above used eight bits for the letter. We'll continue with that, but set up our Memory so it can hold multiple letters. To make things easier to read, I'm removing the switches and grouping each collection of eight bits together.

Type a single letter and you'll see the first eight bits that represent that letter. Type another letter and you'll see the next eight, etc... I recommend starting with a uppercase "A" and an lowercase "z" for the first two letters to make it easy to see the differences in the bits for each letter in Memory.


(Note: Unicode values can be more than eight bits. If you only type U.S. English letters, numbers and spaces you'll only see eight bit entries and everything will line up. However, if you type a character like "≈" you'll see longer strings of bits. We could have set up the examples so everything used a higher number of bits to keep things lined up. In real-world programs you'd absolutely want to do that, but limiting to eight bits for these examples makes them easier to read.)

Introducing Bytes

Grouping bits into sets of eight is so common in the computer world that a group of that size has its own name: byte

Pronounced "bite", it's what's being referred to in words like "Megabyte", "Gigabyte", and "Terabyte".

A Megabyte is 1,048,576 bytes 2. That number may seem weird, but there's a reason it's so specific. It's 2 to the 20th power. That's because computers use sets of bits (which we've seen are based of multiples of 2) to keep track of the memory itself. (We won't be getting into that in this post. For now, it's enough to know that it's just another example of computers using bits for everything.)

Color Bits

A Color Toggle

We've used bits to calculate numbers and to make letters. Now, let's use them to make colors.

We'll start with two colors (Black and Bright Green) and we'll use a single bit to toggle between them. We'll make the color Black when the bit is "0" and Bright Green when the bit is "1".



Four Shades Of Green

To add more colors, we'll add more bits and create a new Memory Map Dictionary that assigns colors to each number:





Total            

B

A

A Shade Slider

Let's replace the two toggle buttons with a slider which makes for a nicer interface for changing between our four colors.

A:
B:
-
Total      



0 3

B

A

All The Shades Of Green

Now, let's use eight bits (aka one byte) for the color. That'll give us Black when all the bits are "0", and 255 varying shades of Green (some of which are so close to black that it's hard to tell they have any color at all, but they do). With that many shades we can make a very smooth color slider.

A:
B:
C:
D:
E:
F:
G:
H:

Total        



0 255

H

G

F

E

D

C

B

A

Red, Green, and Blue Bits

Intro To Color Theory

Our next step is to be able to pick any color we want (not just a shade of green). Before we do that, we need to cover a little bit about how Screens work. We'd need an entire post to really get into the details but for now we can work with this:

RGB Sliders

Providing one slider for Red, one for Green, and one for Blue gives us this:

Red Bits:

Combined
Color



H

G

F

E

D

C

B

A
Green Bits:

Combined
Color



H

G

F

E

D

C

B

A
Blue Bits:

Combined
Color



H

G

F

E

D

C

B

A

Each of the raw Red, Green, and Blue sliders controls its own color which is combined with the other two to produce the final Combined Color.

The Red, Green, and Blue sliders each control eight bits in memory. That gives us a total of 24 bits to control the overall color. Because of the way they double, those 24 bits provide the ability to choose 16,777,216 different colors.

Conclusion

This has been a long post and this feels like a good place to wrap it up. We used our bits to make numbers, letters, and colors. Sounds and music would be a good next topic, but it's really the same thing. You come up with a way to represent whatever your interested in (e.g. the volume for a sound) as a number and then store that number in bits.

I've been programming computers for a few decades. I spent a lot of that time not knowing how bits worked. When I learned about them they seemed unreal. Still kinda feels like that sometimes, if I'm being honest. To be able to do so much with so little feels like sorcery. But, there's no magic. It's just a bunch 1s and 0s.

═══ § ═══

Footnotes And References