How to Calculate IPv4 Addresses
Subnetting IPv4 from scratch: turn a prefix into a subnet mask, find the network and broadcast, then count the hosts. Every step uses the same example.
Updated · Jetorbit
IPv4 subnet formulas at a glance
- Addresses per subnet =
2^(32 − prefix) - Usable hosts =
2^(32 − prefix) − 2 - Block size = 256 − the subnet mask octet that is not 255
- Network address = the nearest multiple of the block size at or below the IP
- Broadcast = network address + block size − 1
- Subnets when splitting a network =
2^n, where n is the number of borrowed bits
Quick example: 192.168.10.77/26 has subnet mask 255.255.255.192, network 192.168.10.64, broadcast 192.168.10.127, and 62 usable hosts. Each step is explained below.
What an IPv4 address is made of
An IPv4 address is 32 bits split into four octets. Each octet holds 8 bits, so it ranges from 0 to 255. Octets are written in decimal and separated by dots.
From left to right, the bits in an octet are worth 128, 64, 32, 16, 8, 4, 2, and 1. For example 77 = 64 + 8 + 4 + 1, so it is written 01001101.
Every IPv4 address has two parts. The left part is the network the device sits on. The right part is the host, the device within that network. The prefix or subnet mask sets where one ends and the other begins.
CIDR prefixes and subnet masks
The prefix comes after a slash, such as /24 or /26. It says how many bits from the left belong to the network. This notation is called CIDR (Classless Inter-Domain Routing).
A subnet mask is the prefix written as an address. Network bits are 1 and host bits are 0. For /26, that is 26 ones followed by 6 zeros.
A wildcard mask is the inverse of the subnet mask: 255 minus each octet. Router ACLs and some firewalls use it.
Subnet mask octet values
A subnet mask octet can only take one of the values below, depending on how many 1 bits it has. The block size is the step between subnets in that octet.
| 1 bits | octet value | block size |
|---|---|---|
| 1 | 128 | 128 |
| 2 | 192 | 64 |
| 3 | 224 | 32 |
| 4 | 240 | 16 |
| 5 | 248 | 8 |
| 6 | 252 | 4 |
| 7 | 254 | 2 |
| 8 | 255 | 1 |
How to calculate an IPv4 subnet, step by step
Example: a device uses 192.168.10.77/26. Here is how to find its network, broadcast, and host range.
- Turn the prefix into a subnet mask.
/26is three full octets (24 bits) plus 2 bits of the fourth, so the mask is255.255.255.192. - Count the addresses per subnet: 2⁶ = 64, because there are 6 host bits.
- Work out the block size in the partial octet: 256 − 192 = 64. Subnets in the fourth octet start at multiples of 64: 0, 64, 128, 192.
- Find the block the address falls in. 77 is between 64 and 127, so the network address is
192.168.10.64. - The broadcast is the last address in that block:
192.168.10.127. - Usable hosts sit in between:
192.168.10.65to192.168.10.126, which is 64 − 2 = 62 hosts.
The binary way: AND
You can also get the network address by ANDing the IP with the subnet mask, bit by bit. A result bit is 1 only when both bits are 1. For the broadcast, set every host bit to 1.
Subnetting: splitting one network into several subnets
Subnetting borrows host bits and turns them into network bits. Each borrowed bit doubles the number of subnets and halves the addresses in each one.
- Number of subnets =
2^n, where n is the number of borrowed bits. - Hosts per subnet =
2^h − 2, where h is the number of host bits left.
Example: split 192.168.10.0/24 into 4 subnets. That takes 2 bits, since 2² = 4, so the new prefix is /26 with 62 hosts each.
| subnet | network | host range | broadcast |
|---|---|---|---|
| 1 | 192.168.10.0/26 | 192.168.10.1 – 192.168.10.62 | 192.168.10.63 |
| 2 | 192.168.10.64/26 | 192.168.10.65 – 192.168.10.126 | 192.168.10.127 |
| 3 | 192.168.10.128/26 | 192.168.10.129 – 192.168.10.190 | 192.168.10.191 |
| 4 | 192.168.10.192/26 | 192.168.10.193 – 192.168.10.254 | 192.168.10.255 |
If you start from a host count, pick the smallest subnet that still fits. Need 50 hosts? A /26 gives 2⁶ − 2 = 62 and fits, while a /27 gives only 2⁵ − 2 = 30.
IPv4 subnet mask and prefix table
| prefix | subnet mask | addresses | usable hosts |
|---|---|---|---|
| /8 | 255.0.0.0 | 16,777,216 | 16,777,214 |
| /16 | 255.255.0.0 | 65,536 | 65,534 |
| /20 | 255.255.240.0 | 4,096 | 4,094 |
| /21 | 255.255.248.0 | 2,048 | 2,046 |
| /22 | 255.255.252.0 | 1,024 | 1,022 |
| /23 | 255.255.254.0 | 512 | 510 |
| /24 | 255.255.255.0 | 256 | 254 |
| /25 | 255.255.255.128 | 128 | 126 |
| /26 | 255.255.255.192 | 64 | 62 |
| /27 | 255.255.255.224 | 32 | 30 |
| /28 | 255.255.255.240 | 16 | 14 |
| /29 | 255.255.255.248 | 8 | 6 |
| /30 | 255.255.255.252 | 4 | 2 |
| /31 | 255.255.255.254 | 2 | 2 |
| /32 | 255.255.255.255 | 1 | 1 |
/31 is used for point-to-point links between routers, so both addresses are usable (RFC 3021). /32 is a single address, such as one server.
Special IPv4 address ranges
| range | used for |
|---|---|
| 10.0.0.0/8 | private (local networks) |
| 172.16.0.0/12 | private (local networks) |
| 192.168.0.0/16 | private, common on home routers |
| 100.64.0.0/10 | CGNAT: an ISP sharing one public IP among many customers |
| 127.0.0.0/8 | loopback, the device itself |
| 169.254.0.0/16 | link-local: shows up when a device gets no address from DHCP |
| 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24 | documentation and examples |
What about class A, B, and C?
Classes were replaced by CIDR in 1993, but they are still taught. The class comes from the first octet. Class A (1–126) has a default /8 mask, class B (128–191) has /16, and class C (192–223) has /24. With CIDR, a prefix can be any length regardless of class.
Further reading on Jetorbit
These articles are in Indonesian.