IPv4 Addressing, Subnet Masks and Subnetting (CIDR)
- Level
- Beginner
- Time
- 1 h 30 including the exercises
- Tested on
- Calculations checked with ipcalc
Before installing a DHCP server, creating an Active Directory domain or configuring a VLAN, you need to be able to answer one simple question: are these two machines on the same network? This whole article is about answering it without hesitation, in your head or on a sheet of paper.
Objectives
Section titled “Objectives”By the end of this article, you will be able to:
- read an IPv4 address and its subnet mask, in both dotted-decimal and CIDR (
/24) notation; - find the network address, the broadcast address and the range of usable addresses;
- work out how many hosts a network can hold;
- split a network into subnets of equal size, then of different sizes (VLSM).
Prerequisites
Section titled “Prerequisites”None, apart from knowing how to count in powers of 2. A pen and paper help a lot.
The IPv4 address
Section titled “The IPv4 address”An IPv4 address is a 32-bit number. To make it readable, it is split into four octets of 8 bits, written in decimal and separated by dots:
192 .168 .10 .7711000000 .10101000 .00001010 .01001101Each octet therefore ranges from 0 (00000000) to 255 (11111111). An address such as
192.168.10.300 does not exist.
Converting an octet to binary
Section titled “Converting an octet to binary”Each bit in an octet has a weight, from 128 down to 1:
| Weight | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| 77 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 |
To convert 77, take the weights from left to right as long as they fit:
64 fits (remainder 13), 32 and 16 do not, 8 fits (remainder 5), 4 fits (remainder 1), 2 does not, 1 fits.
That gives 01001101.
Network portion, host portion and subnet mask
Section titled “Network portion, host portion and subnet mask”An IP address carries two pieces of information: the network the machine belongs to, and the host, that is, the machine itself within that network. The subnet mask shows where the boundary between the two lies.
The mask is also a 32-bit number. Its bits set to 1 mark the network portion, and its bits set to
0 the host portion. The 1s are always all on the left:
Mask 255.255.255.19211111111.11111111.11111111.11000000└────────── network (26 bits) ─────┘└ host (6 bits)CIDR notation
Section titled “CIDR notation”Writing 255.255.255.192 is long. CIDR notation simply gives the number of 1 bits in the mask:
/26. So you can write the address and its mask in one go: 192.168.10.77/26.
| CIDR | Decimal mask | Host bits | Addresses | Usable hosts |
|---|---|---|---|---|
/8 |
255.0.0.0 |
24 | 16,777,216 | 16,777,214 |
/16 |
255.255.0.0 |
16 | 65,536 | 65,534 |
/22 |
255.255.252.0 |
10 | 1,024 | 1,022 |
/23 |
255.255.254.0 |
9 | 512 | 510 |
/24 |
255.255.255.0 |
8 | 256 | 254 |
/25 |
255.255.255.128 |
7 | 128 | 126 |
/26 |
255.255.255.192 |
6 | 64 | 62 |
/27 |
255.255.255.224 |
5 | 32 | 30 |
/28 |
255.255.255.240 |
4 | 16 | 14 |
/29 |
255.255.255.248 |
3 | 8 | 6 |
/30 |
255.255.255.252 |
2 | 4 | 2 |
The rule behind this table: with n host bits, a network contains 2ⁿ addresses, of which 2ⁿ − 2 are usable by machines.
Why “minus 2”?
Section titled “Why “minus 2”?”In every network, two addresses are reserved:
- the first, where all the host bits are 0, is the network address itself. It is used to refer to the network, for example in a routing table;
- the last, where all the host bits are 1, is the broadcast address. A packet sent to this address is received by every machine on the network.
Neither can be assigned to a machine.
Finding the network of an address: the full method
Section titled “Finding the network of an address: the full method”Let us take 192.168.10.77/26 and look for its network address, its broadcast address and its usable
range.
Method 1: the logical AND in binary
Section titled “Method 1: the logical AND in binary”Line up the address and the mask in binary, then perform a bitwise logical AND: the result is 1 only if both bits are 1. The first three octets of the mask are 255, so they copy the address unchanged. Only the last one needs calculating:
Address 77 01001101Mask 192 11000000 -------- ANDNetwork 64 01000000The network address is 192.168.10.64. For the broadcast address, keep the network portion and set
all the host bits to 1: 01111111, which is 127. The broadcast address is 192.168.10.127.
Method 2: the magic number (faster)
Section titled “Method 2: the magic number (faster)”In practice, you almost never go through binary. Look at the octet where the mask is neither 255 nor 0 (the “interesting” octet), and calculate:
block size = 256 − mask value in that octet
Here, the mask is 192 in the 4th octet: 256 − 192 = 64. So networks start every 64 in that
octet: .0, .64, .128, .192. The address .77 falls between .64 and .127:
| Information | Result |
|---|---|
| Network address | 192.168.10.64/26 |
| First usable address | 192.168.10.65 |
| Last usable address | 192.168.10.126 |
| Broadcast address | 192.168.10.127 |
| Number of hosts | 2⁶ − 2 = 62 |
Both methods obviously give the same result. The first is for understanding, the second for speed.
Can these two machines talk to each other directly?
Section titled “Can these two machines talk to each other directly?”Two machines communicate directly (without a router) only if they are on the same network. To check, work out each one’s network address using its mask.
192.168.1.120/25: block of 128, network192.168.1.0.192.168.1.130/25: block of 128, network192.168.1.128.
They are not on the same network. A ping from one to the other will fail if there is no router
between them, even though their addresses look very similar. This is one of the most common causes of
faults in lab exercises.
Addresses worth knowing
Section titled “Addresses worth knowing”Some ranges have a special role:
| Range | Role |
|---|---|
10.0.0.0/8 |
Private addresses (RFC 1918) |
172.16.0.0/12 |
Private addresses, from 172.16.0.0 to 172.31.255.255 |
192.168.0.0/16 |
Private addresses, the most common in home networks |
127.0.0.0/8 |
Loopback. 127.0.0.1 refers to the machine itself |
169.254.0.0/16 |
APIPA: an address Windows gives itself when it cannot find a DHCP server |
0.0.0.0 |
“No address” or “all addresses”, depending on context (default route 0.0.0.0/0) |
255.255.255.255 |
Limited broadcast to the local network |
Private addresses are not routed on the Internet. They are used in business and lab networks, and translated into a public address by the router or firewall (NAT).
Splitting a network into subnets
Section titled “Splitting a network into subnets”Subnetting means borrowing bits from the host portion and adding them to the network portion. Each borrowed bit doubles the number of subnets and halves their size.
Subnets of equal size
Section titled “Subnets of equal size”Problem: the school has 192.168.1.0/24 and wants 4 separate networks (administration, lab room 1,
lab room 2, servers).
- How many bits to borrow? You need at least 4 subnets: 2² = 4, so 2 bits.
- New mask: 24 + 2 = /26, which is
255.255.255.192. - Size of each block: 256 − 192 = 64 addresses, so 62 hosts.
| Subnet | Network address | Usable range | Broadcast |
|---|---|---|---|
| Administration | 192.168.1.0/26 |
.1 to .62 |
192.168.1.63 |
| Lab room 1 | 192.168.1.64/26 |
.65 to .126 |
192.168.1.127 |
| Lab room 2 | 192.168.1.128/26 |
.129 to .190 |
192.168.1.191 |
| Servers | 192.168.1.192/26 |
.193 to .254 |
192.168.1.255 |
If you had needed 6 networks, 2 bits would not have been enough (only 4 networks). You would have
needed 3 bits (2³ = 8 networks), so a /27 with blocks of 32.
Subnets of different sizes (VLSM)
Section titled “Subnets of different sizes (VLSM)”In real life, not all networks are the same size. Giving 62 addresses to a link between two routers that only uses 2 is wasteful. VLSM (Variable Length Subnet Mask) means choosing a different mask for each subnet.
Problem: starting from 192.168.50.0/24, create:
- an Administration network for 100 computers;
- a Teaching network for 50 computers;
- a Servers network for 20 machines;
- a link between two routers (2 addresses).
The golden rule: always place the largest networks first. Otherwise the blocks no longer fall on valid boundaries.
| Requirement | Smallest suitable mask | Assigned network | Usable range | Broadcast |
|---|---|---|---|---|
| 100 computers | /25 (126 hosts) |
192.168.50.0/25 |
.1 to .126 |
.127 |
| 50 computers | /26 (62 hosts) |
192.168.50.128/26 |
.129 to .190 |
.191 |
| 20 machines | /27 (30 hosts) |
192.168.50.192/27 |
.193 to .222 |
.223 |
| 2 addresses | /30 (2 hosts) |
192.168.50.224/30 |
.225 to .226 |
.227 |
Each network starts just after the broadcast address of the previous one. There is free space left
from 192.168.50.228 to 192.168.50.255 for future needs.
Checking your calculations
Section titled “Checking your calculations”It is useful to know how to calculate by hand, but in a real situation you always check with a tool.
On Linux with ipcalc
Section titled “On Linux with ipcalc”sudo apt install ipcalcipcalc -n 192.168.10.77/26Example output (the -n option removes the colours):
Address: 192.168.10.77 11000000.10101000.00001010.01 001101Netmask: 255.255.255.192 = 26 11111111.11111111.11111111.11 000000Wildcard: 0.0.0.63 00000000.00000000.00000000.00 111111=>Network: 192.168.10.64/26 11000000.10101000.00001010.01 000000HostMin: 192.168.10.65 11000000.10101000.00001010.01 000001HostMax: 192.168.10.126 11000000.10101000.00001010.01 111110Broadcast: 192.168.10.127 11000000.10101000.00001010.01 111111Hosts/Net: 62 Class C, Private InternetThe space in the binary column shows exactly where the boundary between network and host falls.
Viewing a machine’s IP configuration
Section titled “Viewing a machine’s IP configuration”ipconfigip -4 addressWindows shows the mask in decimal (Subnet Mask . . . . . . . . . . . : 255.255.255.0), Linux in CIDR
notation (inet 192.168.1.20/24). It is the same information.
Common mistakes
Section titled “Common mistakes”Assigning the network address or the broadcast address to a machine. With a /26,
192.168.1.64 and 192.168.1.127 are not host addresses. Windows will reject the network address
with an error message; other devices will accept it and then not work.
Forgetting that the mask must be the same everywhere. If one computer is on a /24 and another on
a /25 in the same room, they do not “see” the network in the same way. A ping may work in one
direction and not the other.
Calculating the block on the wrong octet. The magic number is calculated on the octet where the
mask is neither 255 nor 0. With a /20, it is the 3rd octet, not the 4th.
Placing small networks first in VLSM. The blocks end up misaligned and the addresses overlap. Always sort from largest to smallest.
Confusing the number of addresses with the number of hosts. A /24 contains 256 addresses but
only 254 hosts.
Worked exercises
Section titled “Worked exercises”Try first without looking at the solution, using the magic number method.
Exercise 1. Give the network address, broadcast address, usable range and number of hosts for
172.16.33.10/20.
Solution
Mask /20 = 255.255.240.0. Interesting octet: the 3rd, block of 256 − 240 = 16.
The networks are 172.16.0.0, 172.16.16.0, 172.16.32.0, 172.16.48.0… 33 is between 32 and 47.
- Network:
172.16.32.0/20 - Usable range:
172.16.32.1to172.16.47.254 - Broadcast:
172.16.47.255 - Hosts: 2¹² − 2 = 4,094
Exercise 2. What is the minimum subnet mask (in CIDR and in decimal) for a network of 50 computers?
Solution
You need 2ⁿ − 2 ≥ 50. With n = 5: 30, too few. With n = 6: 62, which works.
6 host bits, so 32 − 6 = 26 network bits: /26, which is 255.255.255.192.
Exercise 3. Can the machines 10.10.10.14/28 and 10.10.10.17/28 communicate without a router?
Solution
/28 gives blocks of 16: .0, .16, .32… 10.10.10.14 is in the network 10.10.10.0,
10.10.10.17 in the network 10.10.10.16. No, they are on two different networks.
Exercise 4. Split 192.168.20.0/24 into 8 subnets of equal size. Give the mask and the first
three networks.
Solution
8 = 2³, so borrow 3 bits: /27 (255.255.255.224), blocks of 32, 30 hosts each.
192.168.20.0/27:.1to.30, broadcast.31192.168.20.32/27:.33to.62, broadcast.63192.168.20.64/27:.65to.94, broadcast.95
Exercise 5. A Windows computer shows the address 169.254.12.40. What is happening?
Solution
It is an APIPA address: the computer is set to use DHCP but has received no reply from a DHCP server. Check the cable or the virtual network adapter, then the VLAN, then that the DHCP server is running and that its scope is not full.
Going further
Section titled “Going further”- The wildcard mask. This is the inverse of the subnet mask:
0.0.0.63for a/26. You will come across it in ACLs and in the OSPF configuration of Cisco devices. - Supernetting, or route aggregation. The opposite of subnetting: combining
192.168.0.0/24and192.168.1.0/24into a single route,192.168.0.0/23. - IPv6. Addresses grow to 128 bits and subnetting is almost always done on a
/64. A dedicated article will follow later.
The logical next step after this article is building your practice lab, so you can put these calculations into practice on real virtual machines.
Content licensed under CC BY-SA 4.0, written by the volunteers of the association RootLab.