Understanding Subnets and CIDR via Bitmasking
Network subnetting and Classless Inter-Domain Routing (CIDR) rely on binary bitmasking to segment IP addresses into network and host identifiers. This article explains the binary foundation of IPv4 addressing, how the bitwise AND operation isolates network boundaries, and how CIDR prefix notation represents variable-length subnet masks to optimize address allocation.
The Binary Structure of an IPv4 Address
An IPv4 address consists of 32 bits grouped into four 8-bit sections
called octets, separated by periods. While users interact with decimal
representations such as 192.168.1.10, network hardware
processes these values purely in binary:
- Decimal:
192.168.1.10 - Binary:
11000000.10101000.00000001.00001010
Every IP address contains two components: 1. Network ID: Identifies the specific network segment. 2. Host ID: Identifies the specific device or interface within that network.
Bitmasking is the mechanism used by routers and hosts to determine where the network portion ends and the host portion begins.
The Bitmasking Principle: The Logical AND Operation
A subnet mask is a 32-bit sequence where all network bits are set to
1 and all host bits are set to 0. Subnet masks
always consist of contiguous ones followed by contiguous zeros.
To extract the Network ID from an IP address, network devices perform
a bitwise logical AND operation between the IP address
and the subnet mask. The truth table for the AND operation requires both
inputs to be 1 to output 1:
1 AND 1 = 11 AND 0 = 00 AND 1 = 00 AND 0 = 0
Example of Bitwise Subnetting
Consider an IP address of 192.168.1.75 paired with a
traditional subnet mask of 255.255.255.0:
IP Address: 11000000.10101000.00000001.01001011 (192.168.1.75)
Subnet Mask: 11111111.11111111.11111111.00000000 (255.255.255.0)
------------------------------------------------------------------
Network ID: 11000000.10101000.00000001.00000000 (192.168.1.0)
Because the first 24 bits of the mask are 1, the first
24 bits of the IP address pass through unchanged. Because the remaining
8 bits are 0, the last 8 bits of the IP address are cleared
to 0. The result identifies the base network address as
192.168.1.0.
CIDR Notation as a Shorthand for Mask Length
Classless Inter-Domain Routing (CIDR) replaces rigid address classes
(Class A, B, and C) with a flexible prefix notation. CIDR represents a
subnet mask by appending a forward slash / followed by the
count of leading 1 bits in the mask.
255.255.255.0has 24 consecutive1s \(\rightarrow\)/24255.255.0.0has 16 consecutive1s \(\rightarrow\)/16255.255.255.192has 26 consecutive1s \(\rightarrow\)/26
Using CIDR, the address 192.168.1.75 with a mask of
255.255.255.0 is written compactly as
192.168.1.75/24.
Custom Subnetting and Non-Octet Boundaries
Subnet masks do not need to align with full 8-bit octet boundaries. Network engineers can “borrow” bits from the host portion to create smaller subnets.
For example, a /26 subnet mask applies 26 ones:
- Binary Mask:
11111111.11111111.11111111.11000000 - Decimal Mask:
255.255.255.192
When applying a /26 mask to
192.168.1.75:
IP Address: 11000000.10101000.00000001.01001011 (192.168.1.75)
Subnet Mask: 11111111.11111111.11111111.11000000 (255.255.255.192)
------------------------------------------------------------------
Network ID: 11000000.10101000.00000001.01000000 (192.168.1.64)
The bitmasking operation shows that 192.168.1.75/26
belongs to the 192.168.1.64/26 network segment.
Calculating Network Capacity with Host Bits
The number of remaining 0 bits in a mask dictates the
total capacity of a subnet:
- Total IP addresses: \(2^{(32 - \text{Prefix Length})}\)
- Usable host addresses: \(2^{(32 - \text{Prefix Length})} - 2\)
Two addresses in any subnet are reserved: 1. Network
Address: All host bits set to 0 (used to route
traffic to the subnet). 2. Broadcast Address: All host
bits set to 1 (used to send data to all hosts on the subnet
simultaneously).
For a /26 network, \(32 - 26 =
6\) host bits remain: * Total Addresses: \(2^6 = 64\) * Usable Hosts: \(64 - 2 = 62\) * Network Address:
192.168.1.64 * Broadcast Address:
192.168.1.127 * Usable Host Range:
192.168.1.65 through 192.168.1.126