Whether on your home Wi-Fi or across the internet, your device needs an address to send and receive data. Just like your home has a street address, your computer or phone has an IP address. It’s a unique identifier that helps data know where to go.
There are two types of IP addresses: IPv4 and IPv6.
In this post, we’ll focus on IPv4.
You’ve probably seen something like this before: 192.168.0.1.
That’s an example of IPv4 address.
It’s made of four numbers, called octets, separated by dots. Each octet can go from 0 to 255.
Under the hood, each octet is just 8 bits. So the whole IP address is 32 bits total.
IP address consists of two parts:
To know where to split it, we have to use the subnet mask.
A subnet mask looks like an IP address (e.g. 255.255.255.0).
The difference is that instead of identifying a device, it tells us which part of the IP address is the network, and which part is the host.
It also must start with a group of consecutive 1s and end with consecutive 0s.
This works by using bits:
For example:
255 . 255 . 255 . 0
11111111 . 11111111 . 11111111 . 00000000
In this case, the first 24 bits (the 1s) are for the network, and the last 8 bits (the 0s) are for hosts on that network.
Without subnet masks, computers wouldn’t know if an IP address is on their own network (send directly) or somewhere else (send to the router).
You’ll often see subnet masks written in a shorter form called CIDR (Classless Inter-Domain) notation.
Instead of writing 255.255.255.0, we write it as /24, which means: “The
first 24 bits are 1s.”
This number can go from /0 (no network bits, entire IPv4 space) to /32 (all bits fixed, a single address).
Here are the most common subnet masks:
| CIDR | Binary | Subnet Mask |
|---|---|---|
| /26 | 11111111.11111111.11111111.11000000 | 255.255.255.192 |
| /25 | 11111111.11111111.11111111.10000000 | 255.255.255.128 |
| /24 | 11111111.11111111.11111111.00000000 | 255.255.255.0 |
| /16 | 11111111.11111111.00000000.00000000 | 255.255.0.0 |
| /8 | 11111111.00000000.00000000.00000000 | 255.0.0.0 |
To get network and host parts from IP, we use a bit operation called AND.
It goes bit by bit, comparing the IP address and the subnet mask.
Let’s look at an example:
IP: 192.168.0.1
Mask: 255.255.255.0 (/24 in CIDR)
Convert both to binary:
IP: 11000000.10101000.00000000.00000001
Mask: 11111111.11111111.11111111.00000000
Now we apply the AND operation:
Result: 11000000.10101000.00000000.00000000 (192.168.0.0)
This result is the network address. All devices in this network will share this part.
The rest of the bits (the last 8 in this case) are for hosts, they tell us which device it is on that network.
To find the device (host) number, we do a bitwise NOT on the subnet mask (which flips 1s to 0s and 0s to 1s), and then AND it with the IP:
NOT Mask: 00000000.00000000.00000000.11111111
AND with IP: 11000000.10101000.00000000.00000001
Result: 00000000.00000000.00000000.00000001
So the device number is 1, it’s the first device in this network.
You can use the calculator below to experiment with IP addresses, subnet masks, and see how network and host addresses are determined.
IP addresses are like digital street addresses, and subnet masks are the rules that tell your device which part is the “neighborhood” (network) and which part is the “house number” (host).
With subnet masks, we can slice big networks into smaller, easier-to-manage chunks. This organizes devices and keeps traffic efficient, your computer knows when to talk directly to a neighbor and when to go through the router.
So the next time you see something like 192.168.1.42/24, you’ll know that:
Find this post helpful? Subscribe and get notified when I post something new!