Number Systems

You may have heard some people refer to the data inside a computer as "ones and zeroes," but what does that actually mean? Computers understand the world using a number system called binary, which represents values using the digits 1 and 0. How? Well, to understand binary, let's start by getting a refresher on a more familiar number system first.

Counting with Digits and Places

Let's start by thinking about the basic counting system we use every day. Most of us use this system so intuitively that we no longer even think about how it works, but we all did have to learn the basics once upon a time. Consider the following number:

The number 5,023

This number is five thousand and twenty-three. How do we know that? Well, the number is made up of four different digits: 5, 0, 2, and 3. Just those digits isn't enough, however. After all, the number 5,023 is not the same as 2,305. The order also matters.

The number 5,023, with each position labeled as 1's, 10's, 100's, and 1000's.

When you first learned to count, you were likely taught about number places. The position of a digit in the number has a meaning. Starting from the right-hand side and going to the left, you have the ones place, the tens place, the hundreds place, the thousands place, and so on.

The digit determines how many copies of each place value we have. For example, this number has 2 in the tens place, so this number includes 2 tens, or 20.

In fact, when determining the total value of a number, we can write out the value of each digit in this way and add them together. So the number 5,023 is composed of:

5 thousands
0 hundreds
2 tens, and
3 ones

If you wanted to write that out mathematically using multiplication, it would look like this:

Each digit multiplied by its place value

This shows us how to calculate the individual value of each digit based on the place that it is in. If you were to then add those values together, you would get the original number, five thousand and twenty-three.

The multipled values added together

The Decimal System

Okay, that's great and all, but why do we care? That was a lot of work to get back to a number that we already understand intuitively! Well, not so fast: let's break down this system a little further.

First of all, let's look again at the digits. This system uses the digits 0 - 9. Why? Because when we hit the number 10, we put a new tally (add one) in the next place up, and reset the current place to 0 to start over again. We only need to use 0 - 9 because any larger than that and our places determine the value instead of the individual digit.

And what about these places? Well, if you look closely at them, you may notice a pattern in how they increase.

Each place shown as a power of 10

Each of these places is actually a power of 10!

1 = 100
10 = 101
100 = 102
1000 = 103

This pattern is where our counting system — the decimal system — gets its name. The prefix "deci" is latin for 10. Because of these powers of ten, the decimal system is also known as the "base 10" system.

With this in mind, consider the following question: what would you need to do to make a numbering system that did NOT count in base 10? How would it be different from the decimal system? What places and digits would you need?

Binary

The prefix "bi" in "binary" means 2. Binary is a base 2 number system. In other words, where our normal counting system would use the number 10, binary uses the number 2 instead.

For digits, the decimal system used the values 0 - 9 because they go up to 10 and then reset. Binary uses the digits 0 and 1, because they only go up to 2 before resetting. This means that all numbers written in binary will be composed of only 0's and 1's.

The number 101101

For places, instead of using powers of 10 like decimal, binary uses powers of 2. This is a little less intuitive for most people, who are not used to counting in base 2. So often it helps to write out the place values so you know what they are.

1 = 20
2 = 21
4 = 22
8 = 23
16 = 24
32 = 25

If you've never worked in base 2 before, basically you start with 1 on the right and then double the previous number for each next place up.

Now that we know what the places are, we can use this to calculate value of numbers just like before by combining the digits and the places using multiplication.

The previous number with power of 2 place values shown

Just like with the decimal system, we can find the value of any given number by taking each digit and multiplying it by the value of the place it is in. This is pretty easy to do, since binary only has the digits 1 and 0, which are easy to multiply. Once we're done, we can add all the values together to get our final total. So, to calculate the value of this number:

1 thirty-two
0 sixteens
1 eight
1 four
0 twos
1 one

Or, to write it out with multiplication:

The binary digit values written out and then added together

By doing this, we have successfully converted a binary number into a decimal value that we understand. We now know that "101101" is the binary representation of the decimal value 45. Any number can be represented using the binary number system, just like it can for decimal.

Why Is Binary Useful?

Okay, big deal, so what? Why do we care that we can represent a number in base 2 instead of base 10? Why is that valuable?

Different base number systems can be really useful in different contexts. For example, binary is extremely useful because it can represent any number at all using only the digits 0 and 1. This means that any system that has only two values can still be used to represent all numbers.

Consider for a moment a computer. What is a computer? When you take it down to its base parts, it's really only a series of electrical circuits combined together. Electrical circuits have two states: on, or off. But knowing what we now know about binary, this means that a combination of enough two-state electrical circuits like this can represent any number in existance. That's pretty powerful!

If you start assigning certain numbers to represent letters, that means that your simple on-and-off electrical circuit can display text as well. With enough numbers, you can represent colors, pictures, sounds - anything you want to display! So in the end, it's true: all the data in a computer is really just 1's and 0's.

That's the power of binary. But what about other number base systems? With the rules we unlocked, it's pretty easy to imagine any system with any arbitrary base number! You could write in base 5, where you use the digits 0 - 4 and every place is a power of 5. Octal — the base 8 system — is actually pretty common in programming, as it makes certain binary calculations a little easier. As you might expect, the digits used for octal are 0 - 7, and the places are all powers of 8.

But what about systems where the base is LARGER than 10? How do they work?

The Hexadecimal System

One common large-base system is hexadecimal, or the base 16 system. As should be obvious by now, all of the places in this system are powers of the number 16. But what about the digits?

Obviously the digits can still start at 0, but after we reach digit 9, we run out of arabic numerals to represent the values. After all, our digit values need to go all the way up to 15. So how can we represent that?

Hexadecimal (also known as just "hex") solves this problem by using letters where the numbers run out. So in hex, the digit representing 10 is A. 11 is represented by B, and so on, out to 15, which happens to be F. This means a typical hexadecimal number looks something like this:

The hex number e71f

This may feel confusing at first, but just remember that each letter represents a single digit, like any of the numbers do. They're just digits that happen to be larger than 9. To convert this number into a decimal value that we're more familiar with, you follow the same steps as you would with any other base:

The hex number e71f with its digits multiplied out by its places

When you add all of the multipled values together, you learn that the hexadecimal number "e71f" represents the decimal value 59,167.

Notice that the number e71f has only four digits, whereas the same value in decimal has 5. This is one of the benefits of using a large base system like hex: you can represent large numbers in a much shorter amount of space. The bigger the number, the more you save by representing it in hex. Because of this, large values such as colors (which are composed of three different values between 0 and 255) are often represented using hexadecimal.

Conclusion

2, 8, 10, and 16 are common number bases used in programming. Other base numbers sometimes crop up in a variety of real-world situations as well: for example, base 60, 12, and 24 are common for increments used to measure time.

Small bases are great when you want to represent numbers but don't have very many digits to work with. Large bases are great for representing a number in a small amount of space, but require new symbols whenever your digits rise above 9.

Any positive whole number 2 or greater can be used as the base for a number system. Which number you choose depends on your particular needs.