IPv4 addressing and subnetting: the arithmetic you learn to do in your head
What the 32 bits mean, how a mask splits them into network and host, the special ranges, and a method for cutting a block into subnets and reading an address in a few seconds without a calculator.
On this page
Subnetting has a reputation for being the hard part, and it is not. It is one idea — a line drawn through 32 bits, with network on the left and host on the right — plus a handful of numbers worth memorising so you never have to convert to binary again. Once the line is real to you, everything else in networking gets easier, because almost every routing decision, ACL, DHCP scope and firewall rule is written in terms of where that line sits.
The map
Read this first when short on time. Every branch is a section below.
An address is 32 bits pretending to be four numbers
An IPv4 address is a 32-bit number. Writing it as four decimal numbers separated by dots is purely a convenience for humans; the network never sees dots.
192.168.1.10 is really:
192 168 1 10
11000000 . 10101000 . 00000001 . 00001010Each of the four groups is an octet: eight bits, so 0 to 255. To convert either way you only ever need one row of numbers, the place values of eight bits:
128 64 32 16 8 4 2 1To read binary to decimal, add the place values wherever there is a 1. 11000000 is 128 + 64 = 192. To go the other way, subtract greedily from the left: 168 minus 128 leaves 40, minus 32 leaves 8, minus 8 leaves 0, so bits at 128, 32 and 8 are set: 10101000. That is the entire binary skill this subject needs.
The crucial idea is that the 32 bits are split into two fields. The left part identifies which network the host is on; the right part identifies which host on that network. Routers care only about the left part — that is what makes routing tables small enough to exist.
- Octet
- One of the four 8-bit groups of an IPv4 address. Networking says "octet" rather than "byte" because early hardware did not agree that a byte was eight bits.
- Prefix
- The network part of an address, and the usual modern word for a network entry: "the 10.1.0.0/16 prefix".
- Dotted decimal
- The human notation, four decimal octets separated by dots. Purely presentational.
The mask: where the line is drawn
A subnet mask is another 32-bit value whose ones mark the network bits and whose zeros mark the host bits. The ones are always contiguous and always start from the left, so a mask is fully described by counting them — which is what CIDR notation does. 255.255.255.0 has 24 ones, so it is written /24.
Because the ones are contiguous, only nine values can ever appear in a mask octet, and knowing them by heart removes almost all the arithmetic:
| Bits | Binary | Decimal | Block size |
|---|---|---|---|
| 0 | 00000000 | 0 | 256 |
| 1 | 10000000 | 128 | 128 |
| 2 | 11000000 | 192 | 64 |
| 3 | 11100000 | 224 | 32 |
| 4 | 11110000 | 240 | 16 |
| 5 | 11111000 | 248 | 8 |
| 6 | 11111100 | 252 | 4 |
| 7 | 11111110 | 254 | 2 |
| 8 | 11111111 | 255 | 1 |
The "block size" column is the one that does the real work later: it is 256 minus the mask value, and it tells you how far apart consecutive subnets are.
Getting the network address
The network address is the address with every host bit set to 0. Formally you AND the address with the mask; in practice you do it one octet at a time and only the octet where the line falls needs any thought.
For 192.168.1.10/24, the line falls on an octet boundary, so it is immediate: network 192.168.1.0, broadcast 192.168.1.255, usable hosts 192.168.1.1 to 192.168.1.254.
Three addresses always exist in a subnet and only one of them is usable by a host:
- Network address — all host bits 0. Names the subnet, cannot be assigned to a host.
- Broadcast address — all host bits 1. Reaches every host in the subnet, cannot be assigned.
- Everything in between — usable host addresses, hence the
2^h - 2formula.
Two hosts on the same wire with different masks is a classic silent fault. Each computes a different idea of what is local, so traffic works in one direction and vanishes in the other. Always check the mask, not just the address, when something is reachable one way only.
- Cisco IOS takes masks in dotted decimal on interfaces (
ip address 10.1.1.1 255.255.255.0) but prints routes in slash notation, so you convert constantly. - Linux and cloud consoles use CIDR almost exclusively — an AWS VPC is created as
10.0.0.0/16and carved into subnets from there. - ACLs on IOS use a wildcard mask, which is the subnet mask inverted:
/24becomes0.0.0.255. Getting these backwards is one of the most common configuration errors there is.
- CIDR
- Classless inter-domain routing. The scheme that replaced address classes with an explicit prefix length, letting networks be any size that is a power of two.
- Wildcard mask
- An inverted subnet mask used by Cisco ACLs and OSPF network statements: zeros mean "must match", ones mean "don't care".
Ranges that already mean something
Some parts of the address space carry rules, and recognising them at a glance saves a great deal of time.
Classes, and why they still get mentioned
Before CIDR, the first bits of an address fixed the mask. That system is gone — no modern device routes by class — but the letters survive as shorthand for the ranges.
| Class | First octet | Default mask | Networks | Hosts each |
|---|---|---|---|---|
| A | 1 to 126 | /8 | 126 | 16,777,214 |
| B | 128 to 191 | /16 | 16,384 | 65,534 |
| C | 192 to 223 | /24 | 2,097,152 | 254 |
| D | 224 to 239 | — | Multicast | |
| E | 240 to 255 | — | Reserved, experimental | |
127 is missing from class A on purpose: the whole 127.0.0.0/8 block is loopback, so 127.0.0.1 and 16 million of its neighbours all mean "this machine".
Private addresses
Three blocks are reserved for use inside organisations and are never routed on the public internet. Every network you have ever configured at home or in an office uses one.
| Block | Range | Size | Typical use |
|---|---|---|---|
| 10.0.0.0/8 | 10.0.0.0 – 10.255.255.255 | 16.7 million | Enterprises and cloud VPCs |
| 172.16.0.0/12 | 172.16.0.0 – 172.31.255.255 | 1 million | Mid-size networks, Docker defaults |
| 192.168.0.0/16 | 192.168.0.0 – 192.168.255.255 | 65,536 | Homes and small offices |
The 172.16 block is the one people get wrong. It is a /12, not a /16, so it covers 172.16 through 172.31 — and 172.32.1.1 is a public address belonging to someone else.
Addresses with a job
127.0.0.0/8— loopback. Never leaves the host.169.254.0.0/16— link-local, assigned automatically when DHCP fails. Seeing one on a client is a diagnosis, not a configuration: the host asked for an address and nobody answered.224.0.0.0/4— multicast.224.0.0.5and.6are OSPF,224.0.0.10is EIGRP.255.255.255.255— limited broadcast, delivered to everyone on the local wire and never forwarded.0.0.0.0/0— as a route, "everything", the default route. As a source address, "I do not have one yet", which is how a DHCP client starts.
- Docker puts containers on
172.17.0.0/16by default, which collides spectacularly on corporate networks already using 172.16/12 — a very common first day of pain. - AWS reserves five addresses in every subnet, not two: network, broadcast, plus the first three for the gateway, DNS and future use.
- Carrier-grade NAT uses
100.64.0.0/10, a fourth quasi-private block, so an ISP can NAT its own customers before they reach the internet.
Subnetting: borrowing bits
Subnetting means taking a block and moving the network/host line to the right, converting host bits into network bits. Each bit you borrow doubles the number of subnets and halves the hosts in each.
Suppose you have 192.168.1.0/24 — one network, 254 usable hosts — and you need four separate networks for four departments. Borrow two bits (2^2 = 4) and the mask becomes /26.
| Subnet | Network | First host | Last host | Broadcast |
|---|---|---|---|---|
| 1 | 192.168.1.0 | 192.168.1.1 | 192.168.1.62 | 192.168.1.63 |
| 2 | 192.168.1.64 | 192.168.1.65 | 192.168.1.126 | 192.168.1.127 |
| 3 | 192.168.1.128 | 192.168.1.129 | 192.168.1.190 | 192.168.1.191 |
| 4 | 192.168.1.192 | 192.168.1.193 | 192.168.1.254 | 192.168.1.255 |
Notice the shape of that table. The networks are 0, 64, 128, 192 — multiples of 64 — and 64 is the block size, which is 256 - 192, where 192 is the last octet of the mask 255.255.255.192. Every subnetting question reduces to finding this number.
The two formulas, and what they are counting:
- Subnets =
2^nwherenis the number of bits borrowed. Two borrowed bits can be 00, 01, 10, 11 — four combinations, four subnets. - Usable hosts =
2^h - 2wherehis the number of host bits left. Six host bits give 64 combinations, minus the network address and the broadcast address, so 62.
Worth memorising, because it turns most questions into recognition:
| Prefix | Mask | Block | Usable hosts | Typical use |
|---|---|---|---|---|
| /24 | 255.255.255.0 | 256 | 254 | A normal user VLAN |
| /25 | 255.255.255.128 | 128 | 126 | Half a /24 |
| /26 | 255.255.255.192 | 64 | 62 | A small department |
| /27 | 255.255.255.224 | 32 | 30 | A server segment |
| /28 | 255.255.255.240 | 16 | 14 | A rack or a DMZ |
| /29 | 255.255.255.248 | 8 | 6 | A few devices |
| /30 | 255.255.255.252 | 4 | 2 | A router-to-router link |
| /31 | 255.255.255.254 | 2 | 2 | A point-to-point link, no waste |
| /32 | 255.255.255.255 | 1 | 1 | A loopback or one host in an ACL |
The /31 is the exception to the minus-two rule. On a point-to-point link there is nobody to broadcast to, so both addresses are usable and the two ends simply take one each. The /32 is the other exception: a single address, used for router loopback interfaces and for matching exactly one host in an access list.
- Cisco routers conventionally get a
/32loopback address that is always up even when every physical interface is down — OSPF and BGP use it as a stable router identity. - Kubernetes hands each node a
/24from a large pod network, which is subnetting used as an allocation strategy rather than a broadcast-domain boundary. - ISPs hand business customers a
/29of public space — six usable addresses — which is why small companies ration their public IPs so carefully.
VLSM: stop giving a two-host link 254 addresses
Variable-length subnet masking simply means using different mask lengths in the same network, sizing each subnet to what it actually needs. Without it, every subnet is the same size as the largest one, and a network of 254-address blocks spends most of its space on links that carry two routers.
The method is always the same: sort the requirements from largest to smallest, then carve each one off the front of the remaining space. Doing it in that order guarantees each subnet lands on a boundary its own block size allows.
Take 192.168.10.0/24 and these needs:
| Segment | Hosts needed | Prefix chosen | Range |
|---|---|---|---|
| Sales LAN | 100 | /25 (126 usable) | 192.168.10.0 – 192.168.10.127 |
| Engineering LAN | 50 | /26 (62 usable) | 192.168.10.128 – 192.168.10.191 |
| Servers | 25 | /27 (30 usable) | 192.168.10.192 – 192.168.10.223 |
| Management | 10 | /28 (14 usable) | 192.168.10.224 – 192.168.10.239 |
| WAN link A | 2 | /30 | 192.168.10.240 – 192.168.10.243 |
| WAN link B | 2 | /30 | 192.168.10.244 – 192.168.10.247 |
| Spare | — | /29 | 192.168.10.248 – 192.168.10.255 |
One /24 now serves six segments with room left over. Done the naive way — every subnet a /25 because Sales needs one — you would have run out after two.
flowchart TD
S["List every segment<br/>with its host count"] --> T["Sort largest first"]
T --> U["Pick the smallest prefix<br/>that fits the count"]
U --> V["Assign it at the next<br/>free block boundary"]
V --> W{"More left?"}
W -->|Yes| U
W -->|No| X["Write down network,<br/>range and broadcast for each"]
Size for growth, not for today. A department of 50 people that gets a /26 is fine until phones, laptops, tablets and access points each take an address. Counting devices rather than people, and leaving 30 percent headroom, avoids a renumbering exercise nobody enjoys.
Doing it fast: the method
Here is the routine for answering "what network is 172.16.140.200/22 on, and what is its range?" in about ten seconds, without binary.
- Find the interesting octet. That is the octet the prefix length lands in: /9–/16 is the second, /17–/24 the third, /25–/32 the fourth. A
/22lands in the third octet. - Write the mask value for that octet. A
/22is255.255.252.0— 22 ones is 8 + 8 + 6, and 6 bits is 252 from the table. - Block size = 256 minus that value. 256 − 252 = 4. Subnets in the third octet therefore start at 0, 4, 8, 12 … 140, 144.
- Count up to the address. The third octet is 140, which is itself a multiple of 4, so the network is
172.16.140.0. - The next subnet starts one block later at
172.16.144.0, so the broadcast is one below it:172.16.143.255. - Usable hosts are everything between:
172.16.140.1to172.16.143.254.
Every subnetting question you will ever be asked is one of those six steps. The only part worth drilling is step 2, and that is the nine-row table above.
The reverse: summarisation
Summarising means replacing several prefixes with one that covers them all, so a routing table stays small. The rule: the summary mask is the number of leading bits all the networks share.
Summarise 10.1.4.0/24, 10.1.5.0/24, 10.1.6.0/24 and 10.1.7.0/24. The first two octets are identical, so 16 bits are shared already. In the third octet: 4 is 00000100, 5 is 00000101, 6 is 00000110, 7 is 00000111. The first six bits are the same and the last two vary, giving 16 + 6 = 22 shared bits. The summary is 10.1.4.0/22.
Sanity check it with block size: a /22 has block size 4 in the third octet, so 10.1.4.0/22 covers 4, 5, 6 and 7. Exactly the four networks, nothing extra. If a summary covers ranges you do not own, you have summarised too aggressively and will attract traffic you cannot deliver.
Summarisation only works on blocks that are aligned and contiguous. 10.1.5.0/24 through 10.1.8.0/24 is four networks but cannot be summarised as one /22, because 5 is not a multiple of 4. This alignment rule is why address plans allocate on power-of-two boundaries from the start.
- Internet routing lives on summarisation: the global BGP table holds around a million prefixes, and would hold hundreds of millions if providers advertised every customer network separately.
- OSPF area design exists so that a well-planned address scheme can be summarised at the area border, keeping a change inside one area from rippling everywhere.
- Cloud VPC peering refuses overlapping CIDR blocks, which is why organisations that grew by acquisition end up NATing between their own networks.
- VLSM
- Using different prefix lengths within one address block so each subnet is sized to its need.
- Supernetting
- Summarisation: representing several contiguous networks with one shorter prefix.
- Block size
256 minus the mask value in the interesting octet. The spacing between consecutive subnets, and the fastest route to every subnetting answer.
Recap
- An IPv4 address is 32 bits split into a network part and a host part; dotted decimal is just presentation.
- The mask says where the split falls: contiguous ones from the left, counted by CIDR notation.
- Only nine values ever appear in a mask octet — 0, 128, 192, 224, 240, 248, 252, 254, 255.
- Every subnet has an unusable network address (all host bits 0) and broadcast address (all host bits 1), hence
2^h - 2. - Block size is
256 minus the mask value, and subnets always begin at multiples of it. - Classes are dead as a routing mechanism but survive as names for ranges; 127 is loopback, not class A.
- Private space is 10/8, 172.16/12 and 192.168/16; 172.16/12 stops at 172.31, not 172.16.
- A 169.254 address means DHCP failed, and is a diagnosis rather than a setting.
- Borrowing
nbits gives2^nsubnets and halves the hosts each time. - VLSM sizes each subnet to its need; always allocate largest first so blocks stay aligned.
- A
/30is the classic WAN link with two usable addresses; a/31gives two with no waste; a/32is one host or a loopback. - Summarisation replaces contiguous, aligned prefixes with the count of their shared leading bits.
Questions
Say the answer out loud before opening it.
What does a subnet mask actually do?
It marks which bits of the address identify the network and which identify the host, with ones for network bits and zeros for host bits.
- The ones are always contiguous from the left, so the mask can be written as a single count — the CIDR prefix length.
- A host ANDs its own address with the mask to learn its network, and does the same with a destination to decide whether it is local or needs the gateway.
This is why a mismatched mask between two hosts on the same wire breaks traffic in one direction only: each side computes a different answer to "is this local?".
How many usable hosts are in a /26, and why is it not 64?
62, because six host bits give 64 addresses and two of them are reserved.
- All host bits zero is the network address, which names the subnet.
- All host bits one is the broadcast address for that subnet.
- The general formula is
2^h - 2, wherehis the number of host bits.
The exception is a /31, where point-to-point links use both addresses because there is nobody else on the wire to broadcast to.
Given 172.16.140.200/22, what are the network, broadcast and usable range?
Network 172.16.140.0, broadcast 172.16.143.255, usable 172.16.140.1 to 172.16.143.254.
- A /22 is 255.255.252.0, so the interesting octet is the third and the block size is 256 − 252 = 4.
- Third-octet subnets start at 0, 4, 8 … 140, so 140 is itself a network boundary.
- The next subnet is 172.16.144.0, so the broadcast is one address below it.
A /22 holds 1,022 usable addresses, which is four /24s worth — a common size for a whole floor or a wireless client pool.
You need six subnets from 192.168.20.0/24. What mask, and what are the first two subnets?
Borrow three bits for a /27, giving eight subnets of 30 usable hosts; the first two are 192.168.20.0/27 and 192.168.20.32/27.
- Two bits would give only four subnets, so three is the minimum that covers six.
- A /27 is 255.255.255.224, block size 256 − 224 = 32.
- Subnets run 0, 32, 64, 96, 128, 160, 192, 224 — two spare for growth.
If those six segments have very different sizes, VLSM would serve them better than eight identical /27s.
What are the three private address ranges, and which one do people get wrong?
10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16; the middle one is the one people misread.
- 172.16.0.0/12 covers 172.16.0.0 through 172.31.255.255, not just the 172.16 range.
- Anything from 172.32 upward is public space belonging to someone else.
- All three are defined in RFC 1918 and are never routed on the public internet.
There is also 100.64.0.0/10 for carrier-grade NAT, which behaves privately but is not RFC 1918 space.
A machine has the address 169.254.12.90. What does that tell you?
That it tried to get an address from DHCP, got no reply, and self-assigned a link-local address.
- The block 169.254.0.0/16 is reserved for exactly this, and such addresses are never routed.
- The host can talk to other self-assigned hosts on the same wire and nothing else.
- Likely causes are a dead DHCP server, a missing relay on the router, a VLAN misconfiguration on the switch port, or a disconnected uplink.
It is a useful signal precisely because it is not a configuration anyone chose — it only ever appears when something upstream failed.
What is VLSM and why does it matter?
Using different mask lengths inside the same address block so each subnet is sized to its actual need.
- Without it, every subnet is as large as the largest one, and most of the space is wasted on small segments.
- A router-to-router link needs two addresses, so a /30 or /31 rather than a /24.
- The method is to sort requirements largest first and carve each from the remaining space, which keeps every block aligned.
VLSM requires a routing protocol that carries the mask with each prefix, which is why classless protocols such as OSPF replaced the classful ones.
Why is a /30 the traditional choice for a router-to-router link, and what replaced it?
A /30 gives exactly two usable addresses, one per router, wasting only the network and broadcast addresses; a /31 does the same job with no waste at all.
- On a point-to-point link there is no broadcast to send, so the reserved addresses serve no purpose.
- RFC 3021 defines the /31 for this case, and modern IOS supports it on point-to-point interfaces.
- Using /31s halves the address consumption of a WAN with many links.
Many networks still use /30 out of habit and tooling compatibility, because some devices and monitoring systems still assume a broadcast address exists.
Summarise 10.1.4.0/24, 10.1.5.0/24, 10.1.6.0/24 and 10.1.7.0/24.
10.1.4.0/22.
- The first 16 bits are identical across all four.
- In the third octet, 4, 5, 6 and 7 share their first six bits and differ only in the last two, so 22 bits are common.
- Checking with block size: a /22 has block size 4 in the third octet, covering exactly 4 through 7.
The same four networks starting at 10.1.5.0 could not be summarised into a single /22, because 5 is not a multiple of 4 — summaries must be aligned as well as contiguous.
What is a wildcard mask and how do you build one?
An inverted subnet mask used by Cisco ACLs and OSPF network statements, where 0 means "this bit must match" and 1 means "ignore this bit".
- Subtract each mask octet from 255: /24 becomes 0.0.0.255, /26 becomes 0.0.0.63.
0.0.0.0matches exactly one host, and255.255.255.255matches anything — which is whatanyexpands to.- They can be non-contiguous, allowing tricks like matching only odd-numbered subnets, though that is rarely worth the confusion.
Writing a subnet mask where a wildcard belongs is one of the most common ACL errors, and it usually fails open or closed in a way that is not obvious until traffic tests it.
How do you convert 10101100 to decimal without a calculator?
Add the place values where a bit is set: 128 + 32 + 8 + 4 = 172.
- The eight place values are always 128, 64, 32, 16, 8, 4, 2, 1.
- Going the other way, subtract greedily from the left: 172 − 128 = 44, − 32 = 12, − 8 = 4, − 4 = 0.
In practice you rarely need this, because the block-size method answers subnetting questions in decimal — but it is the check you fall back on when a mask is unusual.
Two hosts on the same switch, 192.168.1.10/24 and 192.168.1.20/25, cannot talk reliably. Why?
Their masks disagree, so they compute different ideas of what is local.
- The /25 host thinks the local network ends at 192.168.1.127, so .10 is local and reachable directly.
- The /24 host thinks everything to .255 is local, so it also ARPs directly.
- In this particular pair both are inside the /25 so it may work, but move one host above .127 and traffic goes to the gateway in one direction and directly in the other.
Asymmetric reachability that depends on which address you pick is the fingerprint of a mask mismatch, and it is why DHCP handing out the mask centrally is safer than configuring it by hand.
What does 0.0.0.0/0 mean, and where does it appear?
A prefix with zero network bits, so it matches every possible address — the default route.
- In a routing table it is the entry used when nothing more specific matches, which is how a host reaches the internet.
- Because routing uses longest prefix match, a /0 always loses to any other matching route, so it is safe as a catch-all.
- As a source address, 0.0.0.0 means "I have no address yet", which is what a DHCP client sends in its initial discover.
A device that listens on 0.0.0.0 is binding to all local addresses rather than one specific interface, which is the same "any" idea applied to sockets.
Why does a /24 per VLAN remain the default habit even when the segment holds twenty devices?
Because private address space is effectively free, and a /24 makes the third octet a readable VLAN identifier that anyone can decode at a glance.
- 10.20.30.0/24 for VLAN 30 lets an address tell you its site, function and VLAN without a lookup.
- Uniform sizes make ACLs, DHCP scopes and summarisation trivial to write.
- The cost is only wasted private addresses, which nobody bills for.
The habit reverses where space actually is scarce — public address blocks and cloud VPCs peered with other networks — and there VLSM discipline returns immediately.
What is the difference between a broadcast address and 255.255.255.255?
A subnet's broadcast address reaches every host on that specific subnet and can be routed towards it if a router is configured to allow it; 255.255.255.255 is a limited broadcast that never leaves the local wire.
- 192.168.1.255 is the directed broadcast for 192.168.1.0/24.
- Directed broadcast forwarding is disabled by default on Cisco routers because it was the basis of the old smurf amplification attack.
- DHCP discovers use 255.255.255.255, since the client does not yet know what subnet it is on.
Both are layer 3 broadcasts and both map onto the layer 2 broadcast MAC ff:ff:ff:ff:ff:ff, which is what actually makes every switch port receive a copy.