Counting on Memories

Last week, I talked about how computers store information in memory. Here’s a brief TLDR refresher:

  • Every computer contains memory in the form of RAM, in which data is stored.
  • Computer RAM can be thought of as a large file cabinet filled with individual drawers.
  • Each drawer in the cabinet can hold one number from 0 to 255 (more on why later).
  • Each drawer is labelled, and can be located, by its address.

Each drawer in the file cabinet is a single memory location in RAM, and every variable you use in your program corresponds to one or more locations in RAM.

I also ended that article with the question: how does the computer store a number bigger than 255? Let’s start answering that question now.

Adjustments

First, let’s make a small adjustment to our file cabinet analogy. Our file cabinet is still a single row of drawers, but instead of arranging them vertically, let’s start thinking of them horizontally:

Horizontal files
Analogy of computer RAM as a horizontal file cabinet

That’s better! It will help as we move forward with this discussion.

Filling Out Forms

Let’s continue building knowledge through analogies. Hopefully you’ve seen forms like this one:

Block form
Form using boxes for each letter

Each box on the form is meant to hold one number or letter. If the form asks for your birthday, it normally provides two boxes for the month (which can be one through twelve) and two for the day number (which can be one through 31). If there was only single box for the month, then people born in October, November, or December (months 10, 11, and 12) wouldn’t be able to fill out the form. Two boxes are needed to store a number which might be two digits long.

The same thing happens in computer memory. Each memory location “box” can store a number between 0 and 255. If you need to store a number in memory which is bigger than 255, your program asks for two memory locations in which to store it.

So why is 255 so special?

Switch On, Switch Off

Computers are, at their heart, very simple machines. They are basically a huge set of electrically controlled switches. Each switch can be in one of two different states, either on or off. Think of this as corresponding to the numbers zero (0) for off, and one (1) for on. Computer RAM is nothing more than an organized set of these switches.

Imagine two switches side by side. How many different ways are there for those switches to be set? If each switch can be one or zero, then there are four different ways to set them:

Switch #1 Switch #0
Combination 0 0 0
Combination 1 0 1
Combination 2 1 0
Combination 3 1 1

What if we had three switches? Here’s that table showing all eight combination:

Switch #2 Switch #1 Switch #0
Combination 0 0 0 0
Combination 1 0 0 1
Combination 2 0 1 0
Combination 3 0 1 1
Combination 4 1 0 0
Combination 5 1 0 1
Combination 6 1 1 0
Combination 7 1 1 1

NOTE: One thing you may have noted in the tables above is that the first combination is called combination #0, and the first switch is switch #0.

This is because humans normally count things of which we we have at least one, so we start with the number 1.

Computers, however, always have to deal with the possibility that there is nothing there, so they begin with the number 0. This corresponds to the situation where the first combination is always all switches set to zero.

Every time you add a switch, the number of combinations is doubled, in this case from four to eight. This pattern continues no matter how many switches you add – each new switch you add doubles number of possible combinations. If you create this table for eight different switches, you will find you can store 256 different combinations, from all zero’s to all one’s.

NOTE: There is a mathematical shortcut to all this doubling. If you know how many switches you have, you can figure out the number of combinations by calculating:

Power of 2 Math

where x is the number of switches. For eight switches, you calculate 2 to the 8th power to get 256.

Because the first combination is always combination #0, the last combination will be the number of combinations minus one. For an eight switch table, this is combination #255, which happens to be the biggest number we can store in a single memory location.

In computer terms, each of the switches is called a bit. A group of eight bits is called a byte. Therefore, a single byte can store a number between 0 and 255, because that’s how many different ways you can arrange eight bits.

All Those Bits Matter

So what happens when we want to store a number bigger than 255? Remember the form above – someone born on February 12 would use a only a single box to write the month number “2”, but would use two boxes to write the day number “12”. So to store a number bigger than 255, you would use two memory locations. Because two memory locations contain sixteen (16) different bits, you can store 2 to the 16th different combinations, which is 65,536 combinations. That corresponds to a number between 0 and 65,535.

You may wonder how this works for negative numbers, or really REALLY big numbers, or fractions, but one step at a time. We’ll get there, I promise, but next time, we’ll talk about how you can store data which isn’t numeric at all.