At the network layer, logical addressing is the foundation of global routing, ensuring that every connected host or router interface is uniquely identifiable. This guide details the mathematics, architectural transitions, and subnetting operations of the Internet Protocol version 4 (IPv4) addressing system.
An IPv4 address is a 32-bit binary identifier that uniquely and universally defines the connection of a host or router interface to the global Internet. The total number of unique addresses possible—known as the address space—is defined by:
To make these 32-bit addresses human-readable, three standard notation systems are used:
0x.Dotted-Decimal: 129. 11. 11. 239
│ │ │ │
Binary: 10000001 00001011 00001011 11101111
│ │ │ │
Hexadecimal: 0x81 . 0x0B . 0x0B . 0xEFA valid dotted-decimal IPv4 address must adhere to the following constraints:
111.56.045.78 is invalid).Network devices extract address boundaries, subnet configurations, and target networks using bitwise calculations. The three fundamental logical operations are NOT, AND, and OR.
Inverts every individual bit of an address or mask.
Outputs a if and only if both input bits are .
Outputs a if at least one of the input bits is .
AND Operation (Used to find Network Address):
Address: 11000000 10101000 00001010 01010101 (192.168.10.85)
Mask: 11111111 11111111 11111111 00000000 (255.255.255.0)
────────────────────────────────────────────
Result: 11000000 10101000 00001010 00000000 (192.168.10.0)Introduced during the early development of the Internet, classful addressing divided the entire 32-bit address space into five distinct classes: A, B, C, D, and E.
The class of an address is determined by analyzing the first few bits of the first octet (the most significant bits):
Class-checking Tree (Continuous Verification):
[MSB Bit]
├── 0 ──────────────────────────────────────────────────────────► Class A (0.0.0.0 to 127.255.255.255)
└── 1 ── [2nd Bit]
├── 0 ───────────────────────────────────────────────► Class B (128.0.0.0 to 191.255.255.255)
└── 1 ── [3rd Bit]
├── 0 ────────────────────────────────────► Class C (192.0.0.0 to 223.255.255.255)
└── 1 ── [4th Bit]
├── 0 ─────────────────────────► Class D (224.0.0.0 to 239.255.255.255)
└── 1 ─────────────────────────► Class E (240.0.0.0 to 255.255.255.255)In classes A, B, and C, the address is split into two logical parts: the network identifier (netid) and the host identifier (hostid).
0) and 3 octets for the hostid. Designed for massive organizations.10) and 2 octets for the hostid. Designed for mid-sized networks.110) and 1 octet for the hostid. Designed for small organizations.Class A: │0│ netid (7 bits) │ hostid (24 bits) │
Class B: │10│ netid (14 bits) │ hostid (16 bits) │
Class C: │110│ netid (21 bits) │ hostid (8 bits) │The static splitting of classes led to massive address exhaustion:
| Class | Prefix | Default Mask | Total Blocks | Addresses per Block | Primary Use |
| :--- | :--- | :--- | :--- | :--- | :--- |
| A | /8 | 255.0.0.0 | | | Large Networks (Huge wastage) |
| B | /16 | 255.255.0.0 | | | Mid-sized Entities |
| | | | | | Small Networks (Often too small) |
| | N/A | N/A | | | Multicast Group Delivery |
| | N/A | N/A | | | Experimental Reservation |
To split a large allocated classful block into smaller physical networks, organizations introduced subnetting. This adds a intermediate level of hierarchy, dividing the hostid into a subnetid and a local hostid:
Standard Two-Level: │ netid (n bits) │ hostid (32-n bits) │
▼ ▼ ▼
Subnetted Three-Level: │ netid (n bits) │ subnetid (s bits) │ hostid (h) │To implement subnetting, a custom subnetwork mask (or subnet mask) is used. The subnet mask has s covering the netid and subnetid, and s covering the hostid.
If a Class B network with default prefix /16 () is divided into subnets, the number of borrowed bits is:
The new subnet prefix length () is:
This yields a subnet mask with eighteen s: 11111111.11111111.11000000.00000000, which translates to 255.255.192.0.
Conversely, supernetting combines several adjacent class C blocks into a single larger block. This shrinks the prefix length to reduce size and overhead in global routing tables.
With the rapid growth of the Internet in the mid-1990s, classful boundaries were abandoned in favor of Classless Inter-Domain Routing (CIDR).
Classless addressing allows arbitrary boundaries instead of fixed 8, 16, or 24-bit blocks. The network is defined by a variable-length prefix and the remaining bits serve as the suffix:
Classless Format: │◄───────── prefix (n bits) ────────►│◄────── suffix (32-n bits) ─────►│CIDR uses slash notation (also known as CIDR notation) to specify the prefix length :
Given any arbitrary address and its classless prefix length , we can calculate the properties of the entire block using three primary equations:
To find the first address (often called the network address), perform a bitwise AND between the address and the network mask : (Alternatively: Set the rightmost suffix bits of the address to all s).
To find the last address (the directed broadcast address), perform a bitwise OR between the address and the bitwise complement (NOT) of the network mask: (Alternatively: Set the rightmost suffix bits of the address to all s).
Suppose an interface address is configured as 110.23.120.14/20.
A prefix of means the mask has twenty s followed by twelve s: Converting to dotted-decimal yields .
Using our first formula:
Convert the third octet of the IP address () to binary to perform the bitwise operations:
Perform the bitwise AND:
Since the first two octets of the mask are all s, they remain unchanged. Since the final octet of the mask is all s, it becomes .
Invert the mask:
Perform the bitwise OR with (or the original address):
Thus, the last address is:
When an organization is allocated a block of classless addresses, it can divide those addresses into smaller subnets using Variable Length Subnet Masking (VLSM).
To ensure a subnetted block routes correctly, two criteria must be met:
An organization is granted the block 14.24.74.0/24 ( total addresses) and must support three subnets:
Visual Layout of Variable-Length Address Blocks:
Address Space [0 to 255]
┌──────────────────────────────────────┬───────────────────┬─────────┬─────────────┐
│ Subnet 1 (128 Addr) │ Subnet 2 (64 Addr)│Subnet 3 │ Reserved │
│ 14.24.74.0 /25 │ 14.24.74.128 /26 │(16 Addr)│ (48 Addr) │
│ │ │/28 │ │
└──────────────────────────────────────┴───────────────────┴─────────┴─────────────┘
0 127 128 191 192 207 208 255This leaves 48 addresses unused (14.24.74.208 to 14.24.74.255) for future growth.
Specific blocks in the IPv4 address space are reserved for special system behaviors and cannot be assigned to public internet hosts:
0.0.0.0/32): Used by a device during its bootstrap process before it has been assigned a valid IP address. It is only valid as a source address.127.0.0.0/8): Packets sent to this range never leave the local machine. They are routed directly back up the local protocol stack for testing and local services (e.g., 127.0.0.1).255.255.255.255): Used to send a packet to every active host on the local physical network. Routers block these packets to prevent broadcast storms from spreading across the wider internet.As the public IPv4 pool depleted, Network Address Translation (NAT) was introduced to slow down address exhaustion. NAT allows an entire private network to share a single public IPv4 address to access the internet.
The Internet Assigned Numbers Authority (IANA) reserved three IP address blocks for private internal networks. These addresses are not routed on the public internet:
10.0.0.0 /8 (Single Class A block)172.16.0.0 /12 (16 contiguous Class B blocks)192.168.0.0 /16 (256 contiguous Class C blocks)Simple IP-only translation requires a unique public IP for each concurrent session, which still limits capacity. Modern NAT networks use Network Address Port Translation (NAPT), mapping both IP addresses and transport-layer port numbers (TCP/UDP) to multiplex many internal devices over a single public IP.
Private LAN Boundary NAT Router Public Internet
[ Host A: 172.18.3.1:5000 ]
│
▼ (Outbound Packet)
Src: 172.18.3.1:5000 ───────────► [ NAT Translation Table ] ────────────► [ Web Server: 8.8.8.8:80 ]
Dst: 8.8.8.8:80 Internal: 172.18.3.1:5000 Src: 200.24.5.8:10001
External: 200.24.5.8:10001 Dst: 8.8.8.8:80
▲
[ Host B: 172.18.3.2:5000 ] │
│ │
▼ (Outbound Packet) │
Src: 172.18.3.2:5000 ───────────────────────┘
Dst: 8.8.8.8:80When a packet leaves the private network, the NAT router rewrites the private source IP and port to its public IP and a unique temporary port. The router maintains this state in its translation table to map incoming response packets back to the correct internal host.
Below is a complete, production-ready Python implementation using standard bitwise operations to compute the first address, last address, and total number of addresses in a classless CIDR block without external libraries.
def calculate_cidr_bounds(ip_str: str, prefix_len: int):
# Convert dotted-decimal string to 32-bit unsigned integer
octets = list(map(int, ip_str.split('.')))
ip_int = (octets[0] << 24) | (octets[1] << 16) | (octets[2] << 8) | octets[3]
# Generate the subnet mask based on the prefix length
mask = ((1 << 32) - 1) ^ ((1 << (32 - prefix_len)) - 1) if prefix_len >
A 32-bit universal identifier represented in dotted-decimal, binary, or hexadecimal notation, yielding a total address space of $2^{32}$ addresses.
An early architecture dividing the address space into five fixed-size classes (A, B, C, D, and E) with rigid boundaries that caused severe address wastage.
A modern, flexible routing scheme utilizing variable-length subnet masks (VLSM) to allocate addresses dynamically based on organizational need.
The mathematical process of splitting a larger network block into smaller, distinct sub-blocks to optimize routing efficiency and limit broadcast domains.
A technology allowing multiple devices in a private local network to share a single public IP address using stateful translation tables.
Test your understanding with 5 questions
An organization is granted the classless block 130.34.12.64/26. What is the last (broadcast) address in this block?
In classful addressing, which class is indicated by a first octet value of 227 in dotted-decimal format?
If a Class B network with a default prefix of /16 is divided into 8 subnetworks, what is the new subnetwork mask?
Which of the following special IP addresses represents the limited broadcast address?
How does Port Address Translation (PAT / NAPT) solve the limitation of a simple IP-only NAT pool?
/24255.255.255.0255.255.240.014.24.74.0/2514.24.74.0 to 14.24.74.12714.24.74.128 (divisible by ).14.24.74.128/2614.24.74.128 to 14.24.74.19114.24.74.192 (divisible by ).14.24.74.192/2814.24.74.192 to 14.24.74.207127.0.0.0/8), local broadcast (255.255.255.255), and private ranges (RFC 1918) are reserved for diagnostic and internal local use.6 Modules
6 Modules