No history yet

Number Systems

The Language of Computers

We think and count in the decimal system, also known as base-10. It uses ten digits (0 through 9). The position of each digit in a number tells us its value. For example, the number 472 is really just a shorthand for 4×102+7×101+2×1004 \times 10^2 + 7 \times 10^1 + 2 \times 10^0. Each place represents a power of 10.

Computers work differently. Their world is built on tiny electronic switches that can be in one of two states: on or off. This two-state system is represented by the binary number system, or base-2. It only uses two digits: 0 (off) and 1 (on). Every piece of information in a computer, from this text to a complex video game, is stored as long strings of these ones and zeros.

Lesson image

Just like in decimal, the position of a digit in a binary number matters. Each position represents a power of 2. So, the binary number 101 isn't one hundred and one. It's 1×22+0×21+1×201 \times 2^2 + 0 \times 2^1 + 1 \times 2^0, which adds up to 4+0+14 + 0 + 1, or 5 in our decimal system.

Helpful Shorthands

Long strings of binary are hard for humans to read and work with. Imagine trying to debug a program by staring at a screen full of 1011010111001101. To make things easier, we often use other number systems as a compact way to represent binary code.

Two common ones are octal (base-8) and hexadecimal (base-16). Octal uses digits 0-7. Hexadecimal is a bit different. Since it needs 16 digits, it uses 0-9 and then the letters A, B, C, D, E, and F to represent the values 10 through 15.

DecimalBinaryOctalHexadecimal
0000000
1000111
2001022
3001133
4010044
5010155
6011066
7011177
81000108
91001119
10101012A
11101113B
12110014C
13110115D
14111016E
15111117F

Why these systems? Because their bases (8 and 16) are powers of 2. This creates a very simple relationship with binary. One octal digit can represent exactly three binary digits (23=82^3 = 8), and one hexadecimal digit can represent exactly four binary digits (24=162^4 = 16). This makes conversion a breeze.

Translating Between Systems

Converting any number to our familiar decimal system is straightforward. You just multiply each digit by its positional value (the base raised to a power) and add everything up.

For example, let's convert the hexadecimal number 1AF to decimal. Remember, A is 10 and F is 15.

1AF16=(1×162)+(10×161)+(15×160)=(1×256)+(10×16)+(15×1)=256+160+15=43110\begin{aligned} 1AF_{16} &= (1 \times 16^2) + (10 \times 16^1) + (15 \times 16^0) \\ &= (1 \times 256) + (10 \times 16) + (15 \times 1) \\ &= 256 + 160 + 15 \\ &= 431_{10} \end{aligned}

To go the other way, from decimal to another base, we use division. Continuously divide the decimal number by the target base and record the remainders. The new number is the sequence of remainders, read from bottom to top. Let's convert decimal 43 to binary.

The real magic happens when converting between binary, octal, and hexadecimal. To go from binary to octal, you group the binary digits in sets of three, starting from the right. For hexadecimal, you group them in sets of four.

Binary: 10110101 Group for Hex (4s): 1011 0101 -> B 5 -> B5_{16} Group for Octal (3s, add leading 0): 010 110 101 -> 2 6 5 -> 265_8

Math in a Binary World

Doing arithmetic in binary is simpler than in decimal because you only have two digits to worry about. Addition follows four simple rules:

0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0, carry the 1

Here’s how you'd add 5 (0101) and 3 (0011) in binary:

\begin{array}{@{}c@{\,}c@{}c@{}c@{}c@{}c} & & \small{1} & \small{1} & & \small{\textit{carry}} \\ & & 0 & 1 & 0 & 1 \quad (5) \\ & + & 0 & 0 & 1 & 1 \quad (3) \\ \hline & & 1 & 0 & 0 & 0 \quad (8) \end{array}

Subtraction can be done with borrowing, like in decimal, but computers use a clever trick to make it easier: complements. This method turns every subtraction problem into an addition problem.

The most common method is called 2's complement, and it's how computers represent negative numbers. To find the 2's complement of a binary number, you first find its 1's complement by flipping all the bits (0s become 1s and 1s become 0s). Then, you simply add 1.

Let's find the 2's complement for 5 (0101):

  1. Flip the bits (1's complement): 1010
  2. Add 1: 1010 + 1 = 1011 So, in a 4-bit system using 2's complement, 1011 represents -5.

Now, if we want to calculate 7 - 5, we can instead calculate 7 + (-5). In binary, that's 0111 + 1011.

\begin{array}{@{}c@{\,}c@{}c@{}c@{}c@{}c} & \small{1} & \small{1} & \small{1} & \small{1} & \small{\textit{carry}} \\ & & 0 & 1 & 1 & 1 \quad (7) \\ & + & 1 & 0 & 1 & 1 \quad (-5) \\ \hline (1) & & 0 & 0 & 1 & 0 \quad (2) \end{array}

We get 10010. Since we are working with 4-bit numbers, the leading '1' is an overflow bit that gets discarded. The remaining 0010 is the binary for 2, which is the correct answer for 7 - 5. By using 2's complement, the computer's circuitry can handle both addition and subtraction using only addition logic.

Binary multiplication and division also exist. They work on principles similar to their decimal counterparts but are simplified by the fact that you're only ever multiplying or dividing by 0 or 1.

Ready to check your understanding?

Quiz Questions 1/6

What is the decimal (base-10) equivalent of the hexadecimal number 1AF1AF?

Quiz Questions 2/6

Why are hexadecimal and octal number systems commonly used in computing?

Understanding how computers see the world in different number systems is the first step in digital logic. These systems are the foundation for everything that comes next.