Table of Contents
Overview
Binary numbers are a way of writing numbers using only two symbols: 0 and 1. Computers use binary internally because it matches the simple on and off states of electronic circuits. In this chapter you will focus on what binary numbers are and how they are structured, without yet converting between decimal and binary or comparing bits and bytes, which will come in later chapters.
The Idea of a Number System
Every number system is based on a set of symbols and a base, also called a radix. The base tells you how many different digit symbols the system uses and how place values grow from right to left.
In the decimal system that you use every day, the base is 10. The digits go from 0 to 9. Each position in a decimal number represents a power of 10. For example, in the decimal number 472, the rightmost digit is units, the next one is tens, then hundreds.
Binary is also a positional number system, but its base is 2, not 10. The digits go from 0 to 1, and each position represents a power of 2 rather than a power of 10. This very simple digit set is what makes binary ideal for electronics, which can easily represent two states.
Digits and Base in Binary
In binary you only have two possible digit values. These are:
0
1
Each of these is called a binary digit. In networking you will very often see the term bit, which is short for binary digit. A bit is simply a single 0 or 1. You will work more with bits and bytes in a later chapter. Here you just need to understand that every binary number is made from a sequence of 0s and 1s.
Because the base is 2, you can think of binary as a counting system that rolls over every time you reach 2, just like decimal rolls over every time you reach 10.
Place Values in Binary
Binary is a positional system, so the value of a digit depends on where it sits in the number. Positions are counted from right to left, starting at position 0. Each position in binary corresponds to a power of 2.
The place values for binary positions look like this:
| Position (from right) | Power of 2 | Place value |
|---|---|---|
| 0 | $2^0$ | 1 |
| 1 | $2^1$ | 2 |
| 2 | $2^2$ | 4 |
| 3 | $2^3$ | 8 |
| 4 | $2^4$ | 16 |
| 5 | $2^5$ | 32 |
| 6 | $2^6$ | 64 |
| 7 | $2^7$ | 128 |
A binary number is built by selecting some of these place values with a digit of 1 and skipping others with a digit of 0. The total value of the number is the sum of all the selected place values.
For example, if you have the binary pattern 101, you have a 1 in position 2, a 0 in position 1, and a 1 in position 0. So you are using the 4 and the 1 place values, and you are not using the 2 place value. The positions and values line up in the same way no matter how long the binary number is.
Writing Binary Numbers
In text, people sometimes add a prefix or suffix to make it clear that a number is binary and not decimal. You may see several styles:
| Representation | Meaning |
|---|---|
1010 | Context decides if this is binary |
0b1010 | Common programming prefix for binary |
1010₂ | Small 2 indicates base 2 |
b1010 | Sometimes used informally in notes |
All of these are ways to write the same sequence of bits. In networking, you will most often see long lines of bits without any decoration when you inspect raw packet data, so you will get used to recognizing binary by context.
Patterns in Binary Counting
Because there are only two digits, binary counting produces very regular patterns. This will be useful later when you work with IP addresses and subnet masks, where certain patterns of bits represent specific ranges or blocks.
Here is a sequence of increasing binary patterns using 4 bits. Only the patterns matter here. You will see how to match them to decimal values in a later chapter.
| Count step | 4-bit pattern |
|---|---|
| 0 | 0000 |
| 1 | 0001 |
| 2 | 0010 |
| 3 | 0011 |
| 4 | 0100 |
| 5 | 0101 |
| 6 | 0110 |
| 7 | 0111 |
| 8 | 1000 |
| 9 | 1001 |
| 10 | 1010 |
| 11 | 1011 |
| 12 | 1100 |
| 13 | 1101 |
| 14 | 1110 |
| 15 | 1111 |
Notice how the rightmost bit alternates 0, 1, 0, 1 and so on. The next one to the left changes every two steps, the next every four steps, and so forth. This predictable structure is one reason binary is so convenient for digital circuits and for network addressing schemes.
General Form of a Binary Number
You can describe any binary number in a mathematical way using its bits and powers of 2. Suppose you have a binary number with bits $b_n b_{n-1} \dots b_2 b_1 b_0$, where each $b_i$ is either 0 or 1 and $b_0$ is the rightmost bit.
The value $V$ of this binary number can be written as a sum of powers of 2:
$$
V = b_0 \cdot 2^0 + b_1 \cdot 2^1 + b_2 \cdot 2^2 + \dots + b_n \cdot 2^n
$$
This expression simply captures the idea that each bit either contributes its place value (if it is 1) or contributes nothing (if it is 0).
Important rule: A binary number is a sum of powers of 2, where each bit selects its corresponding power of 2 if it is 1 and ignores it if it is 0.
You will later use this rule explicitly when you learn to convert between decimal and binary.
Fixed-Length Binary Numbers
In networking, binary numbers are often used with a fixed length. For example, IPv4 addresses internally use 32 bits. Even if the leading bits are 0, the full width is still there.
With fixed-length binary numbers, you can identify the minimum and maximum values that can be represented. For an $n$ bit binary number:
- The smallest pattern is all zeros.
- The largest pattern is all ones.
The largest value with $n$ bits can be written in a compact formula.
If all bits are 1, the value is:
$$
2^0 + 2^1 + 2^2 + \dots + 2^{n-1}
$$
There is a neat result for this sum:
$$
2^0 + 2^1 + 2^2 + \dots + 2^{n-1} = 2^n - 1
$$
Important formula: The maximum value of an $n$ bit binary number is $2^n - 1$.
You will see this formula again when you study how many hosts a subnet can contain and how many addresses a particular prefix length provides.
Why Binary Matters for Networking
Binary numbers are the language of the hardware that transports data over networks. Every bit in a network packet header or payload is part of a binary pattern. Features such as IP addresses, port numbers, flags, protocol identifiers, and options are all encoded in binary.
A good mental picture to keep is that every time you see a tidy decimal layout of a network concept, such as dotted decimal IPv4 addresses or human readable port numbers, there is a binary structure underneath. The chapters that follow will build directly on the idea of binary numbers to show you how data sizes are measured, how numbers are converted between systems, and how binary patterns define the behavior of network devices.