DEV Community

Cover image for Decoding IP Addresses: Mastering Subnet Mask and CIDR Notation
Daniel Idoko
Daniel Idoko

Posted on

Decoding IP Addresses: Mastering Subnet Mask and CIDR Notation

What is an IP Address

In networking, IP addresses serve a straightforward purpose: Identification. They are how IoT (Internet of Things) devices locate each other, whether on a private network or the public internet.

In this article, I will break down the structure on an IP address, how to identify the the Network and Host portions of an IP address. I will also show you an easy way to calculate the CIDR (Classless Inter-Domain Routing) notation.

Even though IPv6 is in widespread use today, I will be using IPv4 for the examples in this article. The concepts are still the same, the only difference is that IPv6 has more bits.

 

IP Address Structure

A typical IPv4 address consists of 32 bits divided into 4 segments using dots (e.g., 11000000.10101000.00000001.00000001). For better human readability each segment is converted to a byte, which represents a decimal value between 0 and 255 (e.g., 192.168.1.1). Each segment cannot exceed 255 because the total number of unique combinations of 8 bits is 256 (including 0).

The 32 bits of an IPv4 address are divided into two parts: the Network portion and the Host portion.

  • The Network portion defines the specific network or sub-network that the address identifies. It is the same for every host on the network.

  • The Host portion defines a node on that network, which identifies the device on the network. It is unique for every host on the network.

Your next question might be: which part of the address is the Network, and which part is the Host? Well, computers also need an answer to this question, and that's where subnet mask comes in.

 

Subnet Mask

Inherent Classes (Classful Addressing)

In the past, IP addresses used inherent classes to determine which bytes of the address represent the network and which parts represent the host'. The class is defined by the first few bits of the leftmost byte. Depending on the class (A, B, C, D, or E), the computer determines how to divide the address into network and host portions.

Classful Addressing Table

Today, IP addresses are defined with an explicit subnet mask (e.g., 255.255.255.0) that identifies the network portion. With subnets, the boundary between Network and Host can fall between bits, not just between bytes as seen in classful addressing.

Key Characteristics of Subnet IP Addresses

  • 1s correspond to the network portion, and the 0s correspond to the host portion. When 255 is converted to binary, the result is 8 bits (11111111). This means the entire byte is part of the network portion.

  • The 1s must be left-most and contiguous. You cannot have a byte that looks like 10101010; only something like 11110000 is valid.

  • At least eight bits must be allocated to the network portion.

  • At least two bits must be allocated to the host portion.

Note that if no subnet is defined for an IP address, the computer will use the class method by default.

How to Identify the Host and Network Portion with Subnet Mask

If an IP address has a subnet of 255.255.255.0, it means the first three bytes are part of the network portion, while the last byte is the host portion. 255 represents 1 byte (8 bits), or 11111111 in binary; it identifies the network portion(s) of an IP address.
If the IP address 192.168.1.1 has a subnet of 255.255.0.0, it means 192.168 is part of the network portion, while .1.1 is part of the host portion.

 

CIDR (Classless Inter-Domain Routing)

As mentioned earlier, the boundary between the network and host can fall between bits. This will result in bytes that are neither 255 nor 0, but in between (the combination of 1s and 0s in binary). CIDR notation is commonly used in this situation. In CIDR notation, the subnet mask is written as /XX, where XX is the number of bits in the network portion.

Example of CIDR (Classless Inter-Domain Routing) notation

192.168.1.0/27

The subnetmask /27 means the first 27 bits are the network portion. I.e The subnet mask in binary will be:
11111111.11111111.11111111.11100000 when converted each byte to decimal, will be 255.255.255.224.
As usual`` the 1s are network while the 0s are host portion.

Top comments (1)

Collapse
 
daniel_idoko profile image
Daniel Idoko

Leave a comment