This content originally appeared on DEV Community and was authored by Naval Kishor Upadhyay
If you’ve ever configured a Wi-Fi router or set up a server, you’ve probably stumbled across something like 255.255.255.0
. That strange number is a subnet mask — and it plays a crucial role in how devices know whether they should talk directly to each other or send data through a router.
Let’s break it down in simple terms.
IP Addresses: The Two-Part Story
Every device on a network has an IP address, and that address has two parts:
- Network part → Identifies which network the device belongs to
- Host part → Identifies the specific device inside that network
Example:
192.168.1.42
-
192.168.1
= Network part -
42
= Host part
But how do we know where the split happens? That’s where the subnet mask comes in.
What Is a Subnet Mask?
A subnet mask tells a device which portion of the IP address is for the network and which portion is for the host.
- Wherever the subnet mask has 1s in binary → that part of the IP is the network
- Wherever it has 0s in binary → that part is the host
Example:
- Subnet mask:
255.255.255.0
- In binary:
11111111.11111111.11111111.00000000
This means the first 24 bits are for the network, and the last 8 bits are for the host.
So 192.168.1.42
belongs to the 192.168.1.0 network, and 42
is the host ID.
CIDR Notation: A Shorter Way to Write It
Instead of writing the whole mask, we often use CIDR notation:
-
/24
→ means 24 ones in the subnet mask →255.255.255.0
-
/16
→255.255.0.0
-
/8
→255.0.0.0
So 192.168.1.42/24
is a compact way of saying: network 192.168.1.0 with 256 possible hosts.
Why Subnet Masks Matter
The subnet mask decides how devices communicate:
- If the destination IP is in the same subnet → send traffic directly (no router needed)
- If it’s in a different subnet → send traffic to the default gateway (usually your router)
Without this logic, every device would flood the entire network asking “Are you there?” — which would be chaotic.
Real-World Example
Let’s say your laptop is 192.168.1.10/24
.
- You want to reach
192.168.1.25
→ Same network, so the laptop sends the packet directly - You want to reach
192.168.2.5
→ Different network, so your laptop forwards the packet to the router (default gateway)
That’s how your machine decides whether the destination is local or needs to be routed elsewhere.
Quick Analogy: Apartment Buildings
Think of IP addresses like apartment addresses:
- Subnet mask = tells you which part of the address is the building number (network) and which is the apartment number (host)
- If two people live in the same building, you can just knock on their door
- If they’re in different buildings, you’ll need to go outside and use the street system (the router)
Final Thoughts
Subnet masks may look intimidating, but they’re just a simple rule: they separate the “network” from the “host.”
The next time you see 255.255.255.0
or /24
, you’ll know it’s the mask telling your device: this is your neighborhood, and here’s when you need to call the router for help.
This content originally appeared on DEV Community and was authored by Naval Kishor Upadhyay