Skip to main content

IP Address & Subnet

IP addresses are crucial for identifying and communicating with devices over a network. They act like digital addresses, allowing devices to locate and interact with each other.

Decimal to Binary IP Address

IP Address

IP Address​

Binary & Decimal Representation​

IP addresses are typically represented in decimal format for human readability, such as 172.168.1.0. However, computers store and process these addresses in binary format.

  • Binary of 172: 10101100

  • How to Calculate: Starting from the right, multiply each bit by 2i (where i is the bit position). For example, 172 is calculated as: [(1 \times 2^7) + (0 \times 2^6) + (1 \times 2^5) + (0 \times 2^4) + (1 \times 2^3) + (1 \times 2^2) + (0 \times 2^1) + (0 \times 2^0) = 172]

Why Use 8 Bits?​

  • The maximum decimal value represented in an 8-bit block is 255 (e.g., 255.255.255.255), and each block in an IPv4 address uses 8 bits.

  • IPv4 addresses consist of four such blocks, resulting in a total of 32 bits.

The Need for IP Addresses​

An IP address uniquely identifies a device over the internet, similar to how your home address allows mail to reach you.

Devices need IP addresses to send and receive data over networks, enabling communication between computers, smartphones, and servers across the globe.

Types of IP Addresses​

Public IP Addresses​

Public IP addresses are required for communication over the internet. These are unique and globally routable.

Private IP Addresses​

Due to the limited number of public IP addresses (around 4 billion), private IP addresses are used within local networks (e.g., home or office networks) to save public IP addresses.

Private IP Address

Private IP Address

Private IP addresses can be the same across different networks. Routers with a unique public IP address facilitate communication over the internet, using Network Address Translation (NAT) to map private IP addresses to the public one.

Note

A router's public IP address is what is exposed to the internet, while private IP addresses remain hidden within the local network.

Example Flow of Communication:

  1. Message Sending from Your Device:

    • Your device has a private IP (e.g., 172.168.1.3).

    • It sends a message to the router.

  2. Message Reaching Your Router:

    • The router has a public IP (e.g., 203.0.113.1) assigned by your ISP.

    • The router uses NAT to map your private IP to its public IP.

  3. Message Sent Over the Internet:

    • The message, now associated with the sender's public IP address (e.g., 203.0.113.1), is sent from the sender’s router over the internet.

    • This public IP is what the outside world sees, effectively hiding the private IPs of the devices behind the router.

    • The destination for this message is the public IP address of the recipient's router, such as 93.184.216.34.

  4. Message Reaching Your Friend's Router:

    • The message reaches your friend's router, which has a public IP address of 93.184.216.34.

    • This public IP is what the recipient's ISP assigns and is visible to all internet traffic.

    • The router, acting as a gateway, receives the message on behalf of the devices within the recipient's local network.

  5. Network Address Translation (NAT) at Your Friend's Router:

    • Your friend's router uses NAT to map the public IP address back to the private IP address of the device within the recipient's network.

    • The router checks its NAT table to find the corresponding private IP address for the incoming message.

    • In this case, it maps the public IP (93.184.216.34) back to the private IP of your friend's device, which might be 172.168.1.4.

  6. Message Delivered to Your Friend's Device:

    • Once NAT has mapped the public IP back to the private IP, the router forwards the message to the specific device with the private IP address 172.168.1.4.

    • The message is then delivered to your friend's laptop.

    • At this point, the original data (e.g., your message) reaches the intended recipient within the local network, completing the communication process.

Static vs. Dynamic IP Addresses​

  • Static IP Addresses: Manually configured and remain constant. Ideal for servers and important devices that need a consistent IP address. Example: AWS servers often use static IP addresses.

  • Dynamic IP Addresses: Automatically assigned by a DHCP server and can change over time. They are typically used for general consumer devices and are cost-effective.

Subnet​

Networks are collections of interconnected devices, and subnets help manage these networks efficiently.

Understanding Subnets​

A subnet, or subnetwork, is a smaller network within a larger network, created for security and efficient IP address allocation.

Example: The IP address 172.168.1.1/24 means that the first 24 bits are the network part, leaving the last 8 bits for host addresses.

  • /24: Small networks, 254 usable addresses.

  • /16: Medium networks, 65,534 usable addresses.

  • /8: Large networks, over 16 million usable addresses.

Classes of IP Address​

Historically, IP addresses were divided into classes for different network sizes.

ClassRange of First OctetDefault Subnet MaskNetwork BitsHost BitsNumber of NetworksHosts per Network
A1 - 126/8824128 (2^7)16,777,214 (2^24 - 2)
B128 - 191/16161616,384 (2^14)65,534 (2^16 - 2)
C192 - 223/242482,097,152 (2^21)254 (2^8 - 2)
D224 - 239Reserved for Multicast----
E240 - 255Reserved for Future Use----

CIDR (Classless Inter-Domain Routing)​

Modern networking often uses CIDR instead of classful networking for more flexibility. CIDR allows for variable-length subnet masking (VLSM), enabling more efficient IP address allocation.

  • CIDR Notation: Instead of fixed classes, CIDR uses a suffix like /17 or /19 to specify the number of network bits.

  • Benefits: CIDR provides better scalability, efficiency, and simplified routing by avoiding the limitations of class-based addressing.