IP Address and Subnet Mask: How to Tell Which Network a Device Is On
The 32-bit IPv4 address, network and host parts, subnet mask and CIDR notation, bitwise AND, network, broadcast and host count, same network or gateway, /26 subnets for VLANs, special addresses, typical mistakes and IPv6.
Every device connected to an IP network has an address, but the address alone is not enough. To communicate, a computer also needs to know which part of its address identifies the network and which part identifies the device: that is the job of the subnet mask. This pair of values drives a decision the computer makes for every packet: deliver it directly to the destination or hand it to the default gateway. This article shows how to read an address and a mask, how to work out the network, the broadcast address and the number of hosts, how to split a network into subnets for VLANs, and which configuration mistakes are the most common.
The IPv4 address: 32 bits in four numbers
An IPv4 address is a 32-bit number. To make it readable it is split into four groups of 8 bits, the octets, and each one is written in decimal, separated from the others by a dot: this is dotted decimal notation. Eight bits represent the values from 0 to 255, so every octet falls within that range and an address such as 192.168.1.300 does not exist.
Take the address used throughout this article, 192.168.1.37. In binary its four octets become 11000000, 10101000, 00000001 and 00100101: 37, for example, is 32 + 4 + 1. The computer always works on the 32 bits; the decimal form is for us.
Network part and host part
The 32 bits do not all play the same role. The leading bits form the network part, which is the same for every device on the same network; the trailing bits form the host part, which is different for each device. It is the same principle as a postal address: the street is shared by many buildings, the house number tells them apart.
The boundary between the two parts is not fixed: it can fall after 8, 16 or 24 bits, or at any other position. That is why the address alone is incomplete. Knowing that a PC has the address 192.168.1.37 does not yet tell us whether the PC at 192.168.1.50 is on the same network.
Subnet mask and CIDR notation
The subnet mask shows where the boundary falls. It is also a 32-bit number, made of a run of 1 bits followed by a run of 0 bits: the 1s cover the network part, the 0s the host part. The mask 255.255.255.0, in binary 11111111.
Writing out the mask is awkward, so we use CIDR notation (Classless Inter-Domain Routing, RFC 4632): after the address we add a slash and the number of 1 bits, called the prefix length. 192.168.1.37/24 is the same as 192.168.1.37 with mask 255.255.255.0; /16 corresponds to 255.255.0.0, /25 to 255.255.255.128. Many systems show this form: on Linux, for example, the ip addr command reports the address as 192.168.1.37/24, while on Windows ipconfig lists the address and the subnet mask separately.
CIDR notation replaced the old A, B and C “classes”, which tied the length of the network part to the first octet. Today the prefix is chosen freely, according to the number of devices to connect.
The bitwise AND operation
To find the network an address belongs to, the computer performs a logical AND between the address and the mask, bit by bit: the result is 1 only if both bits are 1, otherwise it is 0. Where the mask has a 1, the address bit passes through unchanged; where it has a 0, the bit is cleared. The result is the network address.
With a /24 mask the calculation can be done in your head, because the boundary falls between two octets. Binary becomes essential when the boundary falls inside an octet. Take 192.168.1.100/26: the mask is 255.255.255.192 and its last octet, 192, is 11000000 in binary. The last octet of the address, 100, is 01100100. The AND gives 01000000, that is 64: the address belongs to the network 192.168.1.64/26.
Network, broadcast, first and last host
Once the network is known, the other values follow. If the host part has n bits, the network contains 2n addresses. Two of them are reserved:
- the network address, with all host bits set to 0, which identifies the network itself;
- the broadcast address, with all host bits set to 1, which reaches every device on the network.
The addresses that can be assigned to devices, the hosts, are therefore 2n − 2. For 192.168.1.0/24 the host part has 8 bits: 256 addresses, network 192.168.1.0, broadcast 192.168.1.255, first host 192.168.1.1, last host 192.168.1.254, 254 hosts in all. For 192.168.1.64/26 there are 6 host bits: 64 addresses, network .64, broadcast .127, hosts from .65 to .126, that is 62.
| Prefix | Subnet mask | Host bits | Addresses | Usable hosts |
|---|---|---|---|---|
| /8 | 255.0.0.0 | 24 | 16,777,216 | 16,777,214 |
| /12 | 255.240.0.0 | 20 | 1,048,576 | 1,048,574 |
| /16 | 255.255.0.0 | 16 | 65,536 | 65,534 |
| /20 | 255.255.240.0 | 12 | 4,096 | 4,094 |
| /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 |
Usable hosts = 2n − 2, with n host bits. The exceptions are /31 prefixes, used on point-to-point links between routers (RFC 3021), and /32, which denotes a single address.
Same network or gateway: how the PC decides
Every time it has to send a packet, the computer compares the destination with its own network. It applies its own mask both to its own address and to the destination address, then compares the two results.
- Equal results: same network. The PC 192.168.1.37/24 wants to reach 192.168.1.50. The AND with 255.255.255.0 gives 192.168.1.0 in both cases: the destination is on the local network. Using ARP, the PC finds the MAC address of 192.168.1.50 and sends it the frame directly, through the switch.
- Different results: remote network. The PC wants to reach 203.0.113.10. The AND gives 203.0.113.0, which differs from 192.168.1.0. So the PC sends the packet to the default gateway, for example the router 192.168.1.1: it finds the router's MAC address with ARP and delivers the frame to it. The destination IP address in the packet remains 203.0.113.10; the router forwards it towards the destination network.
The default gateway must therefore be on the same network as the PC: it is the only “outside” device the PC can reach without going through anything else. When the address is assigned by DHCP, the mask and the gateway arrive together with the address.
Splitting a /24 into four /26 subnets for VLANs
Lengthening the prefix means dividing a network into smaller subnets. Each bit taken from the host part doubles the number of subnets and halves the size of each one. Going from /24 to /26 takes 2 bits: 22 = 4 subnets of 64 addresses, each with 62 hosts and the mask 255.255.255.192.
The subnets start at multiples of 64: .0, .64, .128 and .192. Each can be paired with a VLAN, for example offices, administration, guests and cameras, with its own gateway on the router or firewall that routes between them: by convention the first host, that is .1, .65, .129 and .193. Note that 192.168.1.70 and 192.168.1.130, although their first three octets are equal, belong to different subnets and communicate only through the gateway.
Special addresses: private, loopback, APIPA
- Private addresses (RFC 1918): 10.0.0.0/8, 172.16.0.0/12 (from 172.16.0.0 to 172.31.255.255) and 192.168.0.0/16. They can be used freely on internal networks, are not routed on the Internet and need NAT to get out.
- Loopback: 127.0.0.0/8, usually 127.0.0.1. It means the computer itself; the packets never leave it for the network.
- APIPA or link-local (RFC 3927): 169.254.0.0/16. The operating system assigns one to itself when it gets no answer from the DHCP server. Seeing it in
ipconfigis a clear symptom: DHCP has not worked. - Documentation addresses (RFC 5737): 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24, reserved for examples, such as the 203.0.113.10 used in this article.
Typical mistakes
- A mask that is too short. A PC configured as 192.168.1.37/16, on a network that is actually a /24, also considers 192.168.2.10 local. It does not hand it to the gateway, tries to reach it with ARP, gets no answer and the communication fails.
- A mask that is too long. With 192.168.1.37/25 the PC's network runs from .0 to .127: to reach 192.168.1.200, which is on the same LAN, the PC goes through the gateway. The traffic takes a pointless detour through the router, provided the router forwards it, and the replies may come back by a different path: an intermittent fault that is hard to diagnose.
- A gateway outside the network. A PC 192.168.1.37/24 with the gateway 192.168.0.1 cannot reach the gateway, which is on another network. Many systems report the error; if the configuration is accepted, the local network works but the Internet does not.
- A network or broadcast address assigned to a host, for example 192.168.1.0 or 192.168.1.255 on a /24: they are not valid addresses for a device.
- A duplicate address. Two devices with the same address compete for the traffic. It often happens when a static address falls inside the range handed out by DHCP.
What about IPv6?
IPv6 addresses have 128 bits, are written in hexadecimal in groups of 16 bits separated by colons, and use only prefix notation, with no dotted mask. The principle is the same: the prefix marks the network part. On LANs the prefix is almost always /64: the first 64 bits identify the subnet and the other 64 the interface (RFC 4291), and stateless address autoconfiguration, SLAAC, requires exactly a /64 (RFC 4862). IPv6 has no broadcast, which is replaced by multicast; the loopback is ::1 and link-local addresses start with fe80::. The prefix 2001:db8::/32 is reserved for examples (RFC 3849).
Conclusions
An IP address only makes sense together with its mask. The bitwise AND of the two gives the network; from there you derive the broadcast address, the first and last host and the number of hosts, 2n − 2. With the same calculation every computer decides whether to deliver a packet directly or to hand it to the default gateway, which must be on its own network. Lengthening the prefix produces smaller subnets, one per VLAN. When something fails, the first things to check are always the same: address, mask and gateway, compared with the network's addressing plan.
The video
Further reading
- Book: LOCAL AREA NETWORKS (LAN) — Volume 2: IP Addressing and Network Services (English edition)
- Book: LOCAL AREA NETWORKS (LAN) — Volume 1: Fundamentals, Technologies, and Infrastructure (English edition)
- Video course: Reti LAN da Zero: Switch, IP, Router, Wi-Fi e Internet (video course in Italian)
- Related article: DHCP: How a Device Gets Its IP Address
- Related article: How Does a Switch Know Where to Send Data?
- Related article: VLANs: Why Separating Networks Improves Security and Organisation
- Related article: NAT and Port Forwarding: How a Private Network Talks to the Internet
- Specialization: LAN Networks
Technical sources
- RFC 791 — Internet Protocol (IPv4): packet format and 32-bit addresses.
- RFC 950 — Internet Standard Subnetting Procedure: subnetting and the subnet mask.
- RFC 4632 — Classless Inter-Domain Routing (CIDR): prefix-length notation and the end of address classes.
- RFC 1122 — Host requirements: comparing the destination with the local network, use of the gateway, loopback addresses 127.0.0.0/8.
- RFC 826 — Address Resolution Protocol (ARP): resolving the MAC address of the destination or of the gateway.
- RFC 1918 — Private addresses: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16.
- RFC 3927 — IPv4 link-local addresses 169.254.0.0/16, self-assigned when DHCP is unavailable (APIPA).
- RFC 3021 — /31 prefixes on point-to-point links.
- RFC 5737 — IPv4 blocks reserved for documentation (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24), used in the examples.
- RFC 4291 — IPv6 addressing architecture: 128 bits, prefixes, 64-bit interface identifier, loopback ::1 and link-local fe80::/10.
- RFC 4862 — IPv6 stateless address autoconfiguration (SLAAC).
- RFC 3849 — The IPv6 prefix 2001:db8::/32 reserved for documentation.
Standards and official documentation available from the respective organisations.