Beginner 16 min read

๐Ÿงฎ IP, Subnet, CIDR & MAC: How Networks Are Split Up

๐Ÿ“˜ What You Will Learn

  • How an IP address is split into a network part and a host part
  • What a subnet and a subnet mask are
  • What CIDR notation (like /24) means
  • How to find the network address, broadcast address and usable hosts
  • How to split one network into smaller subnets
  • The private IP ranges used at home and in offices
  • What a MAC address is and how it differs from an IP address
  • How ARP connects an IP address to a MAC address
  • How to check all of this on Windows and Linux

๐Ÿง  What Is a Subnet?

An IPv4 address has 32 bits, written as 4 numbers like 192.168.1.10. See the What Is an IP Address? IPv4 vs IPv6 Explained tutorial.

Every IP address has two parts:

  • A network part: which network the device is in
  • A host part: which device it is, inside that network

A subnet (short for sub-network) is one such network. Devices in the same subnet can reach each other directly. To reach a different subnet, they go through a router.

Simple meaning:

192.168.1.10
\_______/ \/
  network  host
   part    part

The subnet mask or the prefix length tells you where the network part ends.

๐Ÿ“ฆ Easy Analogy

Think of a postal address.

Colony / street name   =  Network part of the IP address
House number           =  Host part of the IP address
The whole colony       =  Subnet
Colony gate            =  Router (default gateway)
The line that says
"colony name is this long"  =  Subnet mask

Two houses in the same colony can visit each other directly. To reach a house in another colony, you go through the colony gate.

๐Ÿค” Why Do We Need Subnets?

  • Organization: an office can keep Accounts, Staff and Guests in separate networks
  • Less noise: a broadcast (a message to everyone) stays inside its own subnet
  • Security: a router or firewall can control traffic between subnets.
  • No waste: you can give each network only as many addresses as it needs

๐ŸŽญ The Subnet Mask

A subnet mask is a 32-bit number. Its 1 bits mark the network part. Its 0 bits mark the host part.

IP address   192.168.1.10    =  11000000.10101000.00000001.00001010
Subnet mask  255.255.255.0   =  11111111.11111111.11111111.00000000
                                |------ network ------|  |- host -|

Here the first 24 bits are the network part, and the last 8 bits are the host part.

To find the network address, the computer keeps only the network bits and sets all host bits to 0. This is called a bitwise AND of the address and the mask:

11000000.10101000.00000001.00001010   (192.168.1.10)
11111111.11111111.11111111.00000000   (255.255.255.0)
---------------------------------------
11000000.10101000.00000001.00000000   (192.168.1.0)  = network address

Your computer does this to decide: "Is the other device in my subnet? Then talk directly. If not, send it to the default gateway (the router)."

โœ‚๏ธ CIDR Notation

CIDR stands for Classless Inter-Domain Routing. In CIDR notation, you write the number of network bits after a slash:

192.168.1.0/24
  • /24 is the prefix length: the first 24 bits are the network part
  • /24 means the same as the mask 255.255.255.0

CIDR replaced the older system of fixed Class A, Class B and Class C networks. With CIDR, the network part can be any length, so networks can be any power-of-two size. This is defined in RFC 4632.

Common Prefix Lengths

Prefix Subnet mask Total addresses Usable hosts
/8 255.0.0.0 16,777,216 16,777,214
/16 255.255.0.0 65,536 65,534
/24 255.255.255.0 256 254
/25 255.255.255.128 128 126
/26 255.255.255.192 64 62
/27 255.255.255.224 32 30
/28 255.255.255.240 16 14
/29 255.255.255.248 8 6
/30 255.255.255.252 4 2

How the numbers work:

  • Host bits = 32 minus the prefix length. For /24, that is 32 โˆ’ 24 = 8
  • Total addresses = 2 to the power of the host bits. For /24, 2โธ = 256
  • Usable hosts = total minus 2, because two addresses are reserved (see below)

๐Ÿ‘‰ A bigger prefix number means a smaller network. /28 is much smaller than /16.

๐Ÿท๏ธ The Special Addresses in a Subnet

In a normal subnet, two addresses are not given to devices:

Address Host bits Meaning In 192.168.1.0/24
Network address All 0 Names the subnet itself 192.168.1.0
Broadcast address All 1 Reaches every device in the subnet 192.168.1.255

Everything in between is usable: 192.168.1.1 to 192.168.1.254.

The router usually takes one of these, often the first (.1) or the last (.254).

Special case: a /31 subnet (2 addresses) is allowed on point-to-point links between two routers. There, both addresses are used for devices (RFC 3021). A /32 is a single address. You will rarely need either as a beginner.

๐Ÿ”ข Worked Examples

Example 1: 192.168.1.10/24

Item Value
Subnet mask 255.255.255.0
Network address 192.168.1.0
Broadcast address 192.168.1.255
Usable range 192.168.1.1 to 192.168.1.254
Usable hosts 254

Example 2: 192.168.1.130/26

A /26 has 6 host bits, so each subnet has 64 addresses. The subnets start at multiples of 64: .0, .64, .128, .192. The address .130 is in the one that starts at .128.

Item Value
Subnet mask 255.255.255.192
Network address 192.168.1.128
Broadcast address 192.168.1.191
Usable range 192.168.1.129 to 192.168.1.190
Usable hosts 62

Splitting a /24 into Four /26 Subnets

Adding 2 bits to the prefix gives 4 smaller subnets:

Subnet Usable range Broadcast
192.168.1.0/26 .1 to .62 192.168.1.63
192.168.1.64/26 .65 to .126 192.168.1.127
192.168.1.128/26 .129 to .190 192.168.1.191
192.168.1.192/26 .193 to .254 192.168.1.255

๐Ÿ  Private IP Ranges

Some address blocks are set aside for private networks, such as your home or office. They are described in RFC 1918:

Block Range Addresses
10.0.0.0/8 10.0.0.0 to 10.255.255.255 16,777,216
172.16.0.0/12 172.16.0.0 to 172.31.255.255 1,048,576
192.168.0.0/16 192.168.0.0 to 192.168.255.255 65,536

Anyone can use these inside their own network without asking a registry. The same private addresses are reused in millions of networks. On the internet, your router uses NAT to share one public address.

โš ๏ธ Watch out: only 172.16.x.x to 172.31.x.x is private. An address like 172.32.0.1 is not in the private range.

Home routers usually give out 192.168.x.x addresses with a /24 mask through DHCP. See the What Is DHCP? tutorial.

๐Ÿชช What Is a MAC Address?

A MAC address (Media Access Control address) is the hardware address of a network card. Every network card, such as Wi-Fi or Ethernet, has one.

A MAC address is 48 bits long. It is written as 12 hexadecimal digits in 6 pairs. Each pair is one byte (8 bits):

00:11:22:33:44:55      (colons, common on Linux)
00-11-22-33-44-55      (hyphens, common on Windows)

The two writing styles mean the same thing. Hexadecimal uses the digits 0 to 9 and the letters a to f.

The Two Halves

00:11:22 : 33:44:55
\______/   \______/
  OUI      device part
(maker)    (set by the maker)
  • The first 3 bytes (24 bits) are the OUI (Organizationally Unique Identifier). The IEEE assigns it to an organization, such as a company that makes network cards
  • The remaining bits are given by that organization to its devices

Special Bits in the First Byte

The first byte of a MAC address has two special bits.

Bit Name Meaning
Lowest bit Individual/Group 0 = unicast (one device). 1 = group address (multicast or broadcast)
Second-lowest bit Universal/Local 0 = assigned by the maker. 1 = a local address, set by someone else

For example, 02:fc:00:00:00:01 has the first byte 02, which is 00000010 in binary. The lowest bit is 0 (unicast) and the second-lowest bit is 1 (a local address). My test computer, which is virtual, uses such an address.

The broadcast MAC address is ff:ff:ff:ff:ff:ff. A frame sent to it reaches every device on the local network.

Can a MAC Address Change?

The address built into the hardware is set by the maker. But the operating system can use a different one. For example, Apple devices use a private Wi-Fi address, which is a different MAC address for each Wi-Fi network by default. Do not rely on a MAC address as proof of who a device is.

โš–๏ธ IP Address vs MAC Address

IP address MAC address
Length 32 bits (IPv4) 48 bits
Example 192.168.1.10 00:11:22:33:44:55
Given by The network (DHCP or by hand) The maker (the hardware)
Changes when you change networks? Yes Usually no
Works across routers? Yes. It is used to cross networks No. It is used only inside one local network
Layer (see the OSI Model Explained tutorial) Layer 3, Network Layer 2, Data Link

A simple way to remember it: the IP address says where a device is in the network world. The MAC address says which card it is on the local wire.

๐Ÿ”— How ARP Connects IP and MAC

To send data to 192.168.1.20 in the same subnet, your computer needs that device's MAC address. It uses ARP (Address Resolution Protocol):

1. Computer ----> everyone (broadcast): "Who has 192.168.1.20? Tell me your MAC."
2. 192.168.1.20 -> Computer (direct):   "That is me. My MAC is 02:fc:00:00:00:05."
3. Computer saves the answer in a table, so it does not need to ask again.

The saved table is called the ARP cache or neighbor table.

If the other device is in a different subnet, your computer asks for the MAC address of the default gateway (the router) instead, and sends the data to the router.

๐Ÿ‘‰ ARP works on the local network only. See the Switch vs Router vs Hub: What's the Difference? tutorial for how switches use MAC addresses.

๐Ÿ› ๏ธ Try It: Find Your Own IP, Mask and MAC

Step 1: Windows

Open Command Prompt and run:

ipconfig /all

Sample output (yours will differ):

Wireless LAN adapter Wi-Fi:

   Physical Address. . . . . . . . . : 00-11-22-33-44-55
   DHCP Enabled. . . . . . . . . . . : Yes
   IPv4 Address. . . . . . . . . . . : 192.168.1.10(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1
  • Physical Address is the MAC address
  • IPv4 Address and Subnet Mask give you the network (here 192.168.1.0/24)
  • Default Gateway is your router

To see the ARP table:

arp -a

Sample output (yours will differ):

Interface: 192.168.1.10 --- 0x5
  Internet Address      Physical Address      Type
  192.168.1.1           00-11-22-33-44-66     dynamic

Step 2: Linux

ip -br addr
ip link
ip neigh

ip -br addr gives a short list of addresses. ip link shows the MAC address (next to link/ether). ip neigh shows the neighbor (ARP) table.

Output (real, from my test computer. Yours will differ):

lo               UNKNOWN        127.0.0.1/8
eth0             UP             192.0.2.2/24

4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1400 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 02:fc:00:00:00:01 brd ff:ff:ff:ff:ff:ff

192.0.2.1 dev eth0 lladdr 02:fc:00:00:00:05 REACHABLE

How to read it:

  • eth0 has the IP address 192.0.2.2 with prefix /24
  • link/ether 02:fc:00:00:00:01 is the MAC address of eth0
  • brd ff:ff:ff:ff:ff:ff is the broadcast MAC address
  • lladdr 02:fc:00:00:00:05 in the neighbor table is the MAC address of 192.0.2.1, learned through ARP. That is the router
  • REACHABLE means the entry was confirmed recently
  • lo is the loopback interface, which is your own computer talking to itself

On a laptop, the interface name is often wlan0 (Wi-Fi) or enp3s0 (Ethernet). The address 192.0.2.x is an example range reserved for documentation, which my test computer used. Your home network will most likely show 192.168.x.x.

To see the default gateway:

ip route

Output (real):

default via 192.0.2.1 dev eth0
192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.2

Step 3: Calculate a Subnet with Python

You can do the maths with Python's built-in ipaddress module. No installation is needed.

Create a file named subnet_calc.py:

import ipaddress

iface = ipaddress.ip_interface("192.168.1.10/24")
net = iface.network

print("Address          :", iface.ip)
print("Prefix           :", "/" + str(net.prefixlen))
print("Subnet mask      :", net.netmask)
print("Network address  :", net.network_address)
print("Broadcast address:", net.broadcast_address)
print("Total addresses  :", net.num_addresses)
hosts = list(net.hosts())
print("Usable hosts     :", len(hosts))
print("First usable     :", hosts[0])
print("Last usable      :", hosts[-1])

Run it.

Windows:

python subnet_calc.py

Linux:

python3 subnet_calc.py

Output (real, from my run):

Address          : 192.168.1.10
Prefix           : /24
Subnet mask      : 255.255.255.0
Network address  : 192.168.1.0
Broadcast address: 192.168.1.255
Total addresses  : 256
Usable hosts     : 254
First usable     : 192.168.1.1
Last usable      : 192.168.1.254

This matches Example 1 above. Change 192.168.1.10/24 to 192.168.1.130/26 and run it again. You should get the numbers from Example 2.

Step 4: Split a Network into Subnets

Create split.py:

import ipaddress

network = ipaddress.ip_network("192.168.1.0/24")

for subnet in network.subnets(new_prefix=26):
    hosts = list(subnet.hosts())
    print(subnet, "  hosts:", hosts[0], "to", hosts[-1], " broadcast:", subnet.broadcast_address)

Run it (python on Windows, python3 on Linux). Output (real):

192.168.1.0/26   hosts: 192.168.1.1 to 192.168.1.62  broadcast: 192.168.1.63
192.168.1.64/26   hosts: 192.168.1.65 to 192.168.1.126  broadcast: 192.168.1.127
192.168.1.128/26   hosts: 192.168.1.129 to 192.168.1.190  broadcast: 192.168.1.191
192.168.1.192/26   hosts: 192.168.1.193 to 192.168.1.254  broadcast: 192.168.1.255

Step 5: Check If Two Devices Are in the Same Subnet

Create same_network.py:

import ipaddress

net = ipaddress.ip_network("192.168.1.0/24")

for ip in ["192.168.1.55", "192.168.2.55"]:
    print(ip, "in", net, "->", ipaddress.ip_address(ip) in net)

Run it. Output (real):

192.168.1.55 in 192.168.1.0/24 -> True
192.168.2.55 in 192.168.1.0/24 -> False

192.168.2.55 is in a different subnet. A device at 192.168.1.10 would need to go through the router to reach it.

Step 6: See the "Host Bits Set" Error

In Python, run:

python3 -c "import ipaddress; ipaddress.ip_network('192.168.1.10/24')"

(Use python on Windows.) Output (real, last line):

ValueError: 192.168.1.10/24 has host bits set

A network address must have all host bits set to 0. 192.168.1.10/24 is a host address inside a network, not the network itself. To let Python fix it, add strict=False:

ipaddress.ip_network("192.168.1.10/24", strict=False)

This gives 192.168.1.0/24.

โŒ Common Errors & Solutions

Error / Symptom Cause Fix
ValueError: ... has host bits set You gave a host address where a network address is needed Use the network address (192.168.1.0/24), or add strict=False
Two devices in the same office cannot reach each other Different subnets or a wrong mask on one device Compare the network address of both. They must match
A device cannot reach anything outside its subnet The default gateway is missing or wrong Set the gateway to the router's address, inside the device's own subnet
Using the .0 or .255 address for a device These are the network and broadcast addresses Use an address between .1 and .254 (for a /24)
You counted 256 hosts in a /24 You forgot the network and broadcast addresses Usable hosts = total โˆ’ 2 = 254
Subnets overlap Two subnets share some addresses Check each range. Every address can belong to only one subnet
172.32.x.x treated as private Only 172.16.0.0/12 is private Private range is 172.16.0.0 to 172.31.255.255
Two devices show the same IP A static address inside the DHCP pool Use a DHCP reservation or an address outside the pool. See the What Is DHCP? tutorial
The MAC address looks different from the one on the box The device uses a private or locally set address Check the privacy setting of the Wi-Fi network
ip: command not found (Linux) Missing tool on a very small system Install iproute2 (for example sudo apt install iproute2)
python3: command not found (Linux) Python is not installed Run sudo apt install python3
'python' is not recognized (Windows) Python is not installed or not in PATH Install Python and tick "Add to PATH"

๐Ÿ’ก Pro Tip

When a subnet question looks hard, count the host bits first. 32 โˆ’ prefix = host bits. Then 2 to the power of host bits = total addresses. Everything else (the block size, the usable hosts) follows from that.

For quick checks, use the Python ipaddress module instead of counting by hand. It does not make counting mistakes.

๐ŸŽฏ Final Summary

- An IPv4 address = network part + host part
- A subnet = one network. Devices inside talk directly, others go through a router
- Subnet mask: 1 bits = network, 0 bits = host (255.255.255.0 = /24)
- CIDR notation: address/prefix length, for example 192.168.1.0/24
- Host bits = 32 - prefix. Total addresses = 2^(host bits). Usable hosts = total - 2
- Network address = all host bits 0. Broadcast address = all host bits 1
- Private ranges (RFC 1918): 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
- MAC address = 48 bits, 6 pairs of hex digits, first 3 bytes = OUI (maker)
- Broadcast MAC = ff:ff:ff:ff:ff:ff
- IP address works across networks. MAC address works inside one local network
- ARP asks "who has this IP?" and learns the MAC address. The answers are cached
- Windows: ipconfig /all, arp -a | Linux: ip -br addr, ip link, ip neigh, ip route
- Python: ipaddress module for subnet calculations

๐Ÿ“Œ Next Tutorial

How DNS Works: How a name like google.com becomes an IP address.


TechWithJuned ยท Learn โ†’ Execute โ†’ Build

โœ๏ธ Written by TechWithJuned Team

๐Ÿ’ฌ Questions & Discussion

Have a doubt? Ask below โ€” no login needed.

Loading comments...