DEV Community

Cover image for Mastering IP Addresses: A Comprehensive Guide to Understanding, Calculating, and Classifying Network Addresses
Joseph Ibeh
Joseph Ibeh

Posted on

Mastering IP Addresses: A Comprehensive Guide to Understanding, Calculating, and Classifying Network Addresses

Table of Contents

  1. What is an IP Address?
  2. Types of IP Addresses
    • Public IP Address
    • Private IP Address
    • Loopback IP Address
    • Multicast IP Address
  3. Classes of IP Addresses
    • Class A
    • Class B
    • Class C
    • Class D
    • Class E
  4. Netmask and Subnetting
    • What is a Netmask?
    • Subnetting Explained
  5. How to Convert an Octet to Binary
  6. How to Calculate IP Address and Netmask
    • Example Calculation
  7. CIDR Notation
  8. Conclusion.

Introduction
In the world of computer networking, an IP address (Internet Protocol address) is essential for identifying and communicating with devices over a network. Every device that connects to the internet or any other network must have a unique IP address. These addresses are crucial for routing data packets between devices across different networks, ensuring that information reaches its intended destination.
In this article, we'll explore the following:
What IP addresses are
The different types of IP addresses
The concept of netmasks
How to calculate them

1. What is an IP Address?
An IP address is a unique identifier assigned to a device or network interface on a network. It helps route traffic across the internet or local networks, allowing devices to locate and communicate with each other.
There are two main versions of IP addresses:
IPv4 (Internet Protocol version 4): The most widely used format, consisting of four 8-bit octets (32 bits), written as four decimal numbers separated by periods (e.g., 192.168.1.1).
IPv6 (Internet Protocol version 6): Introduced to overcome the limitations of IPv4 (mainly address exhaustion), it uses 128-bit addresses, represented as eight 16-bit groups separated by colons (e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334).

2. Types of IP Addresses

IP Types
IP addresses are categorized based on their usage and purpose. The main types include:

  1. Public IP Address
    A public IP address is routable on the internet. These addresses are assigned to devices or networks by an Internet Service Provider (ISP) and are used for identifying devices on the internet, enabling communication with others over the web. Examples of public IP addresses include:8.8.8.8 (Google's public DNS server) 208.67.222.222 (OpenDNS server)

  2. Private IP Address
    A private IP address is used within local networks, such as in homes, offices, or enterprise environments. These addresses are not routable over the internet but are used for internal communication. The private IP address ranges for IPv4 are:
    10.0.0.0 - 10.255.255.255
    172.16.0.0 - 172.31.255.255
    192.168.0.0 - 192.168.255.255

  3. Static IP Address: A static IP address is fixed and manually configured, such as a server with a permanent IP like 198.51.100.10.

  4. Dynamic IP Address: A dynamic IP address is automatically assigned by a Dynamic Host Configuration Protocol (DHCP) server, such as a home computer with an IP like 192.168.1.5 that changes periodically.

3. Classes of IP Addresses

IP classess
IP addresses are categorized into five classes, each serving different purposes based on the size of the network and the range of addresses required. The classes are as follows:

  1. Class A (0.0.0.0 to 127.255.255.255)

    • Range: 1.0.0.0 - 127.255.255.255
    • Purpose: Designed for large networks, typically used by very large organizations, government bodies, and ISPs.
    • Network Size: Up to 16 million hosts per network.
    • Default Subnet Mask: 255.0.0.0
  2. Class B (128.0.0.0 to 191.255.255.255)

    • Range: 128.0.0.0 - 191.255.255.255
    • Purpose: Suitable for medium-sized networks, such as universities or large businesses.
    • Network Size: Up to 65,000 hosts per network.
    • Default Subnet Mask: 255.255.0.0
  3. Class C (192.0.0.0 to 223.255.255.255)

    • Range: 192.0.0.0 - 223.255.255.255
    • Purpose: Designed for smaller networks, typically used by small businesses and home networks.
    • Network Size: Up to 254 hosts per network.
    • Default Subnet Mask: 255.255.255.0
  4. Class D (224.0.0.0 to 239.255.255.255)

    • Range: 224.0.0.0 - 239.255.255.255
    • Purpose: Reserved for multicast addresses. These are used for one-to-many communication, like streaming media.
    • Network Size: N/A (for multicast communication)
  5. Class E (240.0.0.0 to 255.255.255.255)

    • Range: 240.0.0.0 - 255.255.255.255
    • Purpose: Reserved for experimental use and future applications. These addresses are not used in general networking.
    • Network Size: N/A

4. Netmask and Subnetting

What is a Netmask?
A netmask is a 32-bit number that defines the network portion of an IP address. It works by masking (or blocking out) the host part of the IP address, leaving only the network portion visible. A netmask helps devices determine whether a destination IP address is within the same local network or needs to be routed to another network.
Example:
IP Address: 192.168.1.1
Netmask: 255.255.255.0
In this example, the first 24 bits (192.168.1) represent the network, and the remaining 8 bits (.1) represent the host.

Subnetting
Subnetting is the process of dividing a larger network into smaller subnetworks (subnets). By applying different netmasks, subnetting allows network administrators to efficiently allocate IP addresses and improve network performance. Subnetting is especially useful in large networks where it’s necessary to organize devices into logical groups for efficient routing.

5. How to Convert an Octet to Binary

  1. Understand Binary Numbers
    Binary numbers use only 1s and 0s, and each position represents a power of 2, from right to left:
    128 | 64 | 32 | 16 | 8 | 4 | 2 | 1

  2. Convert the Decimal Number to Binary
    Start with the decimal number and determine how many times each power of 2 fits into it. Write 1 if the power of 2 fits, and 0 if it doesn’t.

Example: Convert 192 to Binary
For 192:
128 fits, so write 1. Subtract 128 → 64 remains.
64 fits, so write 1. Subtract 64 → 0 remains.
Thus, 192 in binary is 11000000.

6. How to Calculate IP Address and Netmask
To calculate the network address from an IP address and netmask, follow these steps:
Convert both the IP address and netmask to binary.
Perform a binary AND operation between the IP address and netmask.
Convert the resulting binary network address back to decimal.

Example Calculation: Converting IP Address and Netmask

We will work with the IP address 192.168.1.10 and the netmask 255.255.255.0.

Step 1: Understand Binary Number.
In binary, each digit represents a power of 2. The place values (from right to left) are:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1
Step 2: Convert the Decimal Number to Binary
Convert 192 to Binary:
The decimal number is 192. Let's figure out which powers of 2 fit into 192:
128 fits into 192, so we write 1.
Subtract 128 from 192 → 192 - 128 = 64 remains.
64 fits into 64, so we write 1.
Subtract 64 from 64 → 64 - 64 = 0 remains.
So, 192 in binary is: 11000000
Convert 168 to Binary:
The decimal number is 168. Let's figure out which powers of 2 fit into 168:
128 fits into 168, so we write 1.
Subtract 128 from 168 → 168 - 128 = 40 remains.
32 fits into 40, so we write 1.
Subtract 32 from 40 → 40 - 32 = 8 remains.
8 fits into 8, so we write 1.
Subtract 8 from 8 → 8 - 8 = 0 remains.
So, 168 in binary is: 10101000
Convert 1 to Binary:
The decimal number is 1. Let's figure out which powers of 2 fit into 1:
1 fits into 1, so we write 1.
No remainder, so the binary representation is just 00000001 (we need 8 bits for each octet).
So, 1 in binary is: 00000001
Convert 10 to Binary:
The decimal number is 10. Let's figure out which powers of 2 fit into 10:
8 fits into 10, so we write 1.
Subtract 8 from 10 → 10 - 8 = 2 remains.
2 fits into 2, so we write 1.
Subtract 2 from 2 → 2 - 2 = 0 remains.
So, 10 in binary is: 00001010
Step 3: Convert the Netmask to Binary
Convert 255 to Binary:
The decimal number is 255. Let's figure out which powers of 2 fit into 255:
128 fits into 255, so we write 1.
Subtract 128 from 255 → 255 - 128 = 127 remains.
64 fits into 127, so we write 1.
Subtract 64 from 127 → 127 - 64 = 63 remains.
32 fits into 63, so we write 1.
Subtract 32 from 63 → 63 - 32 = 31 remains.
16 fits into 31, so we write 1.
Subtract 16 from 31 → 31 - 16 = 15 remains.
8 fits into 15, so we write 1.
Subtract 8 from 15 → 15 - 8 = 7 remains.
4 fits into 7, so we write 1.
Subtract 4 from 7 → 7 - 4 = 3 remains.
2 fits into 3, so we write 1.
Subtract 2 from 3 → 3 - 2 = 1 remains.
1 fits into 1, so we write 1.
Subtract 1 from 1 → 1 - 1 = 0 remains.
So, 255 in binary is: 11111111
Convert 0 to Binary:
The decimal number is 0. Let's figure out which powers of 2 fit into 0:
0 does not fit into any powers of 2, so we write 0 for all bits.
So, 0 in binary is: 00000000
Step 4: Perform the AND Operation
Now we perform the bitwise AND operation (using the IP address and the netmask):
IP Address in Binary:
11000000.10101000.00000001.00001010
Netmask in Binary:
11111111.11111111.11111111.00000000
Perform the AND operation on each bit:
First octet (192) AND 255: 11000000 (IP) AND 11111111 (Netmask) = 11000000 Result: 192
Second octet (168) AND 255: 10101000 (IP) AND 11111111 (Netmask) = 10101000 Result: 168
Third octet (1) AND 255: 00000001 (IP) AND 11111111 (Netmask) = 00000001 Result: 1
Fourth octet (10) AND 0: 00001010 (IP) AND 00000000 (Netmask) = 00000000 Result: 0
Final Network Address:
The result of the AND operation is the network address, which is:
192.168.1.0

Step 5: Calculate the Broadcast Address
To calculate the broadcast address, we take the network address and set all the host bits (the bits corresponding to 0s in the netmask) to 1s.
Network Address in Binary: 11000000.10101000.00000001.00000000
Change the host bits (the last octet, where the netmask is 0) to 1s: 11000000.10101000.00000001.11111111
Now convert the broadcast address back to decimal:
11000000 → 192
10101000 → 168
00000001 → 1
11111111 → 255
Thus, the broadcast address is:
192.168.1.255

7. CIDR Notation
CIDR (Classless Inter-Domain Routing) notation is a shorthand way to specify IP addresses and netmasks. Instead of writing the netmask as four octets, CIDR uses a slash (/) followed by the number of bits used for the network part.
Example:

  • 192.168.1.0/24 means an IP address with a 24-bit network portion (i.e., the netmask is 255.255.255.0)

8. Conclusion
Understanding how IP addresses work, how to calculate network addresses, and how to convert between decimal and binary is crucial for anyone working with networking. Whether you're setting up a local network or managing larger enterprise systems, these fundamental concepts will help you better understand and manage IP addresses and subnets.

Top comments (0)