Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

IP Addressing | Classless Addressing

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

We have introduced IP addressing and classful addressing in the previous post.  

Network Address and Mask

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

Mask – It 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 mask in different classes are :

Class A –

Class B –

Class C –

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

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

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.

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, 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

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


#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)) {
                octet -= (1 << j);
            } else if (bits % 8 != 0) {
    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;


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)) {
                    octet -= (1 << j);// Subtract the value of the bit from the octet
                else if (bits % 8 != 0) {
   // 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)
            "Enter an IP address in classful notation: ");
        string ipAddress = Console.ReadLine();
        Console.WriteLine("Classless address: "
                          + IpClassless(ipAddress));
//The code is contributed by snehalsalokhe


#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: ";
    // This Code is contributed by Himesh Singh Chauhan
    return 0;


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 
      # Means this is Class A IP Address
        return ipaddress+'/8'
      # Means this is a Class B IP Address
        return ipaddress+'/16'
      # Means this is a Class C IP Address
        return ipaddress+'/24'
        print("This is a reserved IP Address, INVALID!!")
ip=input("Enter an IP Address in Classful Notation: ")
# This code is contributed by Himesh Singh Chauhan

Explanation: This program takes an IP address in classful notation as input (e.g. 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:
Classless address:

Example: Given IP Address –, 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 =, first host id =, last host id = and broadcast address =

Below questions have been asked in previous GATE exams on the above topics. 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

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above

My Personal Notes arrow_drop_up
Last Updated : 15 May, 2023
Like Article
Save Article
Similar Reads