Open In App
Related Articles
• Write an Interview Experience
• Computer Network Tutorial

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

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.

### Some Values Calculated in Subnetting:

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

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

## C

 `#include ``#include ``#include ` `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
```

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 2015 Set 3 | Question 48