The question before every send
Before sending anything, a device must decide how to send it. If the target is on the same network, it sends directly: no router, no internet. If not, it hands the packet to the router (the default gateway). The subnet mask is the tool that makes this decision.
Wide by design - scroll it sideways.
What a mask looks like
A mask is written like an IP address. For a /24 network it is 255.255.255.0. The laptop got this value from DHCP (chapter 3). But a mask is not an address: you cannot give it to a device. It exists only to be applied to addresses.
Its purpose is clear in binary. 255 is 11111111 (eight 1s) and 0 is 00000000 (eight 0s). So 255.255.255.0 is twenty-four 1s followed by eight 0s, the exact /24 split from chapter 3:
- 1s = network bits (24)
- 0s = host bits (8)
255.255.255.0 is 11111111 11111111 11111111 00000000 in binary. The first 24 bits are the network part and the last 8 the host part.
Bitwise AND
To use the mask, the device runs a bitwise AND between an IP and the mask: bit 1 of the IP with bit 1 of the mask, bit 2 with bit 2, and so on for all 32 bits. The rule for each pair:
| IP bit AND mask bit | Result | In words |
|---|---|---|
| 1 AND 1 | 1 | AND with 1 keeps the original bit |
| 0 AND 1 | 0 | AND with 1 keeps the original bit |
| 1 AND 0 | 0 | AND with 0 always gives 0 |
| 0 AND 0 | 0 | AND with 0 always gives 0 |
Worked example: the laptop
laptop: 192.168.1.23 AND 255.255.255.0
192.168.1.23192.168.1.23
255.255.255.0255.255.255.0
network address192.168.1.0
192.168.1.23 AND 255.255.255.0 gives the network address 192.168.1.0.
Octet by octet: 192 AND 255 = 192, 168 AND 255 = 168, 1 AND 255 = 1, and 23 AND 0 = 0. The result, 192.168.1.0, is the network the laptop is on. This is the same address CIDR wrote as 192.168.1.0/24.
Same network or not?
Now the laptop wants to send to two places: the phone (192.168.1.40) and Kadé's API (34.87.120.15). It uses its own mask for both, because its own mask is the only one it knows.
phone: 192.168.1.40 AND 255.255.255.0
192.168.1.40192.168.1.40
255.255.255.0255.255.255.0
network address192.168.1.0
192.168.1.40 AND 255.255.255.0 gives the network address 192.168.1.0.
kade-api: 34.87.120.15 AND 255.255.255.0
34.87.120.1534.87.120.15
255.255.255.0255.255.255.0
network address34.87.120.0
34.87.120.15 AND 255.255.255.0 gives the network address 34.87.120.0.
| Target | Target AND mask | Laptop's network | Result |
|---|---|---|---|
| Phone | 192.168.1.0 | 192.168.1.0 | equal → send directly |
| kade-api | 34.87.120.0 | 192.168.1.0 | different → gateway |
What happens next (back to chapter 1)
The answer decides which MAC address goes into the frame, and so what the laptop asks ARP for:
| Target | Same? | Laptop ARPs for | Frame's destination MAC |
|---|---|---|---|
| Printer192.168.1.60 | yes | 192.168.1.60 | 00:1b:a9:44:7c:21the printer itself |
| kade-api34.87.120.15 | no | 192.168.1.1 | 3c:84:6a:10:ee:01the router, which passes it on |
This is why the frame to Kadé's API in chapter 1 carried the router's MAC: the mask check had already said "different network".
Masks of other sizes
A /16 mask is 255.255.0.0: two octets of 1s, two of 0s. Masks do not have to end on an octet boundary. The 1s always fill from the left with no gaps, so a partly-filled octet can only be one of a few values: 128, 192, 224, 240, 248, 252 or 254.
| CIDR | Mask | Last non-255 octet in binary | Addresses |
|---|---|---|---|
| /8 | 255.0.0.0 | 00000000 | 16,777,216 |
| /16 | 255.255.0.0 | 00000000 | 65,536 |
| /20 | 255.255.240.0 | 11110000 | 4,096 |
| /24 | 255.255.255.0 | 00000000 | 256 |
| /25 | 255.255.255.128 | 10000000 | 128 |
| /26 | 255.255.255.192 | 11000000 | 64 |
| /27 | 255.255.255.224 | 11100000 | 32 |
| /28 | 255.255.255.240 | 11110000 | 16 |
| /30 | 255.255.255.252 | 11111100 | 4 |
| /32 | 255.255.255.255 | 11111111 | 1 |
The mask also gives the last address of a range (the broadcast). Flip the mask so 1s become 0s and 0s become 1s (0.0.0.255 for a /24; this flipped form is called a wildcard mask). Then OR it with the network address (OR gives 1 if either bit is 1): 192.168.1.0 OR 0.0.0.255 = 192.168.1.255.
In the cloud: kade-api and kade-db
The same check runs inside GCP. kade-api (10.10.1.10/24, in sn-app) wants to query kade-db (10.10.2.5, in sn-data). kade-api's network is 10.10.1.0. kade-db AND the mask is 10.10.2.0. Different, so kade-api sends to its gateway, 10.10.1.1. Change the values below to test other pairs:
Different networks (10.10.1.0 vs 10.10.2.0). Send to the default gateway (in GCP this is 10.10.1.1).
Summary
| # | Idea | In one line | New terms |
|---|---|---|---|
| 1 | The decision | Same network → direct. Different → default gateway. | subnet mask |
| 2 | Mask format | Looks like an IP; 1s = network bits, 0s = host bits; /24 = 255.255.255.0 | |
| 3 | AND | AND with 1 keeps the bit, AND with 0 clears it | bitwise AND |
| 4 | Network address | IP AND mask = the network the IP is on | |
| 5 | Comparing | Equal network addresses = same network | |
| 6 | Result | Same → ARP for the target. Different → ARP for the router. | |
| 7 | Other sizes | 1s fill from the left; broadcast = network OR wildcard | wildcard mask, OR |
| 8 | Cloud | Same logic in a VPC; latency depends on zone and region, not subnet |
Try it yourself
Your mask
- What to look for:
255.255.255.0, or on Linux the/24after your IP. - Runs on: macOS · Windows: ipconfig · Linux: ip -4 addr
- From section: 2. What a mask looks like
ipconfig getoption en0 subnet_maskHow a LAN target is reached
- What to look for: No
via: sent directly. - Runs on: Linux
- From section: 6. What happens next (back to chapter 1)
ip route get 192.168.1.60How an outside target is reached
- What to look for:
via 192.168.1.1: sent to the gateway. This is the mask decision, visible. - Runs on: Linux
- From section: 6. What happens next (back to chapter 1)
ip route get 34.87.120.15Same check on macOS
- What to look for: A
gateway: 192.168.1.1line for outside targets. - Runs on: macOS
- From section: 6. What happens next (back to chapter 1)
route -n get 34.87.120.15Same check on Windows
- What to look for:
NextHop 192.168.1.1. - Runs on: Windows PowerShell
- From section: 6. What happens next (back to chapter 1)
Find-NetRoute -RemoteIPAddress 34.87.120.15Is an IP inside a range?
- What to look for:
False: kade-db is not in sn-app. - From section: 5. Same network or not?
python3 -c "import ipaddress as i; print(i.ip_address('10.10.2.5') in i.ip_network('10.10.1.0/24'))"