Big Numbers in Memory

For the past few posts, I’ve talked about how computer memory works, how computers store text in memory, and why the biggest number you can store in one memory location is 255.

That last bit may be puzzling to some folks. Computers can surely store numbers bigger than 255, right? Of course they can, so today I’d like to explore how they do it.

Let’s start with our form example from a few weeks ago:

Block form
Form using boxes for each letter

Each box in the form above is design to hold one letter or number. Since zip codes in the U.S. consists of a string of five numbers, the Zipcode area in the form has five different boxes to store those numbers.

Imagine you are filling out a similar form which asks for your age. How many boxes would be needed to hold that number?

Storing Decimal Numbers

To store an age, only one box is necessary for the very young. For most people in the world, two boxes are required, while three will be needed for the estimated 316,000 centenarians in the world. Here’s what that age request might look like:

Age:

Each of the boxes holds a single digit. If you are 52, you would use two of those boxes:

  • One for the “5”
  • One for the “2”

You can rewrite the number 52 as the math expression “50 + 2”. This means we can think of the digit “5” as representing the expression “5 × 10”, and the digit “2” as “2 × 1”.

When I learned about this in elementary school, we talked about places for each number. The digit “2” is in the ones-place, while the digit “5” is in the tens-place. We figure out which digit goes in each place by dividing the number by the place. If we divide 52 by 10, we get 5 with a remainder of 2, which means 5 goes in the tens-place. The remainder goes in the ones-place.

As numbers get larger, more places are added. For example, the number 247 has a “2” in the hundreds-place.

Places: Hundreds Tens Ones
2 4 7

Looking at this more mathematically, you can write the number “247” as “200 + 40 + 7”, or “2×100 + 4×10 + 7×1,” and the number “485” as “4×100 + 8×10 + 5×1.”

Each place you add is a power of ten:

Numbers expressed as powers of ten.
The first three powers of ten.

The multiples get larger as you add numbers to the left. Need to store the number 7,821? Just add one more digit, call it the thousands-place, and rewrite it as “7×1000 + 8×100 + 2×10 + 1×1“:

Ten to the third is one thousand.
One thousand as a power of ten

Each new space you add allows you to write a number ten times bigger than previously allowed. This is because each space has room to write one of ten different digits (“0” through “9”).

Storing Numbers in Memory

So what does this have to do with computers storing big numbers? Computers do the same things as humans when they need to store a big number. They add more space to store them, and do some math to figure out what numbers go where.

Let’s say you want to store the number 1,456 in computer memory. One byte of memory can’t store a number that large, so we have to add some space. Because each byte has room to store one of 256 different numbers, using two bytes allows us to store a number that is 256 times larger than one, or 65,536 different numbers:

Memory: 256’s Ones
? ?

Now the question is what goes where?

Remember we said that when you want to know what digit is in the tens place in the number “52”, you divide 52 by 10, which is 5 with a remainder of 2? To figure out what is in the 256’s place of our computer number, we divide 1,456 by 256, which is 5 with a remainder of 176. So we store 5 in the 256’s place, and 176 in the ones place:

Memory: 256’s Ones
5 176

If we need to store a number larger than 65,535, we continue to add bytes, each one representing a power of 256.

Next time, we’ll look at other ways we can write the numbers, and how everything ties back to binary.