Open In App

Classless Addressing in IP Addressing

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The Network address identifies a network on the internet. Using this, we can find a range of addresses in the network and total possible number of hosts in the network.

Mask is a 32-bit binary number that gives the network address in the address block when AND operation is bitwise applied on the mask and any IP address of the block.

The default masks in different classes are :

  • Class A – 255.0.0.0
  • Class B – 255.255.0.0
  • Class C – 255.255.255.0

Question: Given IP address 132.6.17.85 and default class B mask, find the beginning address (network address).

Solution: The default mask is 255.255.0.0, which means that only the first 2 bytes are preserved and the other 2 bytes are set to 0. Therefore, the network address is 132.6.0.0.

Subnetting

Dividing a large block of addresses into several contiguous sub-blocks and assigning these sub-blocks to different smaller networks is called subnetting. It is a practice that is widely used when classless addressing is done.

A subnet or subnetwork is a network inside a network. Subnets make networks more efficient. Through subnetting, network traffic can travel a shorter distance without passing through unnecessary routers to reach its destination.

Classless Addressing

To reduce the wastage of IP addresses in a block, we use sub-netting. What we do is that we use host id bits as net id bits of a classful IP address. We give the IP address and define the number of bits for mask along with it (usually followed by a ‘/’ symbol), like, 192.168.1.1/28. Here, subnet mask is found by putting the given number of bits out of 32 as 1, like, in the given address, we need to put 28 out of 32 bits as 1 and the rest as 0, and so, the subnet mask would be 255.255.255.240. A classless addressing system or classless interdomain routing (CIDR or supernetting) is the way to combine two or more class C networks to create a/23 or a /22 supernet. A classless addressing system or classless interdomain routing (CIDR) is an improved IP addressing system. In a classless addressing system the block of IP address is assigned dynamically based on specific rules.

Some Values Calculated in Subnetting:

1. Number of subnets : 2(Given bits for mask – No. of bits in default mask)

2. Subnet address : AND result of subnet mask and the given IP address

3. Broadcast address : By putting the host bits as 1 and retaining the network bits as in the IP address

4. Number of hosts per subnet : 2(32 – Given bits for mask) – 2

5. First Host ID : Subnet address + 1 (adding one to the binary representation of the subnet address)

6. Last Host ID : Subnet address + Number of Hosts

C++




#include <bits/stdc++.h>
 
using namespace std;
 
string ip_classless(string ip){
    // Extract the first octet as a string and then convert it to an int using stoi function
   
    int first_octet=stoi(ip.substr(0,ip.find('.')));
    if(first_octet>=0 and first_octet<=127){
        // Means the IP Address in Class A
        return ip+"/8";
    }
    if(first_octet>=128 and first_octet<=191){
        // Means the IP Address is in Class B
        return ip+"/16";
    }
    if(first_octet>=192 and first_octet<=223){
        // Means the IP Address is in Class C
        return ip+"/24";
    }
    return "Reserved IP Address. Invalid.";
}
//Driver Code
int main() {
    // Will store the ip address as an input in ip variable
    string ip;
    cout<<"Enter the IP Address: ";
    cin>>ip;
    cout<<ip_classless(ip)<<endl;
   
    // This Code is contributed by Himesh Singh Chauhan
    return 0;
}


C




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
char *ip_classless(char *ip_address) {
    int i, j, k;
    int octets[4];
    char *mask = (char *) malloc(sizeof(char) * 16);
    int bits = 0;
    for (i = 0, j = 0, k = 0; i < strlen(ip_address); i++) {
        if (ip_address[i] == '.') {
            octets[j++] = atoi(ip_address + k);
            k = i + 1;
        }
    }
    octets[j] = atoi(ip_address + k);
    for (i = 0; i < 4; i++) {
        int octet = octets[i];
        for (j = 7; j >= 0; j--) {
            if (octet >= (1 << j)) {
                bits++;
                octet -= (1 << j);
            } else if (bits % 8 != 0) {
                break;
            }
        }
    }
    sprintf(mask, "%s/%d", ip_address, bits);
    return mask;
}
 
int main() {
    char ip_address[16];
    printf("Enter an IP address in classful notation: ");
    scanf("%s", ip_address);
    printf("Classless address: %s\n", ip_classless(ip_address));
    return 0;
}


Python3




def ip_classless(ipaddress):
    '''
        Will return the Classless IP Address
    '''
    # Alongwith the info of the Default Subnet Mask, info like no. of bits used for NID and no. of bits used for HID can also be specified as they depend on the Class only
    # The no. of Hosts can also be specified on the basis of the Input IP Address
     
    # Gets the First Octet through String Slicing 
    firstOctet=int(ipaddress[:ipaddress.index('.')])
    if(0<=firstOctet<=127):
      # Means this is Class A IP Address
        return ipaddress+'/8'
    elif(128<=firstOctet<=191):
      # Means this is a Class B IP Address
        return ipaddress+'/16'
    elif(192<=firstOctet<=223):
      # Means this is a Class C IP Address
        return ipaddress+'/24'
    else:
        print("This is a reserved IP Address, INVALID!!")
         
         
ip=input("Enter an IP Address in Classful Notation: ")
print(ip_classless(ip))
# This code is contributed by Himesh Singh Chauhan


C#




using System;
 
class Program {
    static string IpClassless(string ipAddress)
    {
      // Initialize some variables
        int j = 0, k = 0;
        int[] octets = new int[4];
        int bits = 0;
        for (int i = 0; i < ipAddress.Length; i++) {
           // If we encounter a '.', extract the octet
            if (ipAddress[i] == '.') {
                octets[j++] = int.Parse(
                    ipAddress.Substring(k, i - k));
              // Convert the octet to an integer and store it
                k = i + 1;
              // Move the starting position of the next octet to the character after the '.'
         
            }
        }
      // Extract the final octet
        octets[j] = int.Parse(ipAddress.Substring(k));
        for (int i = 0; i < 4; i++) {
            int octet = octets[i];
            for (j = 7; j >= 0; j--) {
                if (octet >= (1 << j)) {
                    bits++;
                    octet -= (1 << j);// Subtract the value of the bit from the octet
                }
                else if (bits % 8 != 0) {
                    break;
   // If the current bit is 0 and the bit count is not a multiple of 8, stop counting bits
                }
                }
            }
        }
        return ipAddress + "/" + bits;// Return the input IP address with the bit count appended
    }
 
    static void Main(string[] args)
    {
        Console.WriteLine(
            "Enter an IP address in classful notation: ");
        string ipAddress = Console.ReadLine();
        Console.WriteLine("Classless address: "
                          + IpClassless(ipAddress));
    }
}
//The code is contributed by snehalsalokhe


Explanation:

This program takes an IP address in classful notation as input (e.g. 192.168.0.0) and converts it to classless addressing (CIDR notation) by checking the Class of the IP address and setting the mask(number after ‘/’) on that basis. The resulting CIDR notation is returned by the function ip_classless.

Enter an IP address in classful notation: 192.168.0.0
Classless address: 192.168.0.0/24

Frequently Asked Questions

Question: Given IP Address – 172.16.0.0/25, find the number of subnets and the number of hosts per subnet. Also, for the first subnet block, find the subnet address, first host ID, last host ID, and broadcast address.

Solution:

This is a class B address. So, no. of subnets = 2(25-16) = 29 = 512.

No. of hosts per subnet = 2(32-25) – 2 = 27 – 2 = 128 – 2 = 126

For the first subnet block(means Subnet Number=1), we have subnet address = 172.16.0.0, first host id = 172.16.0.1, last host id = 172.16.0.126 and broadcast address = 172.16.0.127

GATE Previous Year Questions

GATE | GATE CS 2003 | Question 82

GATE | GATE CS 2006 | Question 45

GATE | GATE CS 2007 | Question 67

GATE | GATE CS 2008 | Question 57

GATE | GATE CS 2010 | Question 47

GATE | GATE CS 2012 | Question 21

GATE | GATE CS 2015 Set 3 | Question 48



Last Updated : 20 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads