Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Subnet?
3.
Methods of Subnet Masking
4.
Important formulas to determine the subnets
5.
What is Network Addressing?
6.
Subnet Mask in Computer Networks
7.
Why use Subnet Mask?
8.
What is a Subnet Mask Calculator?
9.
Calculating Subnet Mask from an IP Address
9.1.
Example to Calculate Subnet Mask
10.
Frequently Asked Questions
10.1.
What is 255.255 255.0 in subnet mask?
10.2.
Is 255.255 255.0 or 24?
10.3.
Is 255.255 255.255 IP address valid?
10.4.
Is 10.0 255.255 8 a valid host address?
11.
Conclusion
Last Updated: Mar 27, 2024
Easy

Subnet Mask

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Hello Ninjas, have you ever imagined a city without any addresses or street names - it would be chaos! Similarly, in the world of computer networks with subnet masks, the internet will be a mess. Do you know what a subnet mask is? If not, then don't worry. We will help you to clear all your doubts.

subnet mask

In this article, we will discuss about subnet masks. We will discuss why it is used in computer networks. We will also discuss an example to calculate it. The subnet mask is a very important concept of computer networks. It is used to allow us to use an IP(Internet protocol) address very efficiently. It also divides the complex network into smaller subnetworks. 

Before diving deeper into the concept of the subnet mask, let us understand what a subnet is. 

What is a Subnet?

A subnet in computer networks is a process of dividing huge or complex networks into smaller subnetworks. This smaller network is called a subnet. This process helps networks to improve their performance and security. Each subnet has its unique IPs(IPv4 and IPv6). Devices with the same subnet can communicate easily with each other.

subnet

To understand a subnet, we can take an example of a large organization with multiple departments. These departments have their own set of devices installed in them. By creating different subnets for each and every department, communication will be easy. 

The network administrator can ensure that the devices within each department can communicate with each other easily. Along with this communication, they will also be able to maintain a high level of security between the departments. There are several benefits of subnetting:

  • Efficient use of IP addresses
     
  • Improved network performance
     
  • Better network security
     
  • Easier network management
     
  • More control over network traffic
     

There is a very important concept used in subnetting, i.e., network addressing. Let us discuss about that.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Methods of Subnet Masking

The methods of subnet masking are:

  1. Variable-Length Subnet Masking (VLSM): Allows for the use of different subnet mask lengths within the same network, resulting in efficient use of IP address space.
     
  2. Prefix Length Notation: Represents subnet masks using CIDR notation (e.g., /24) to simplify and standardize subnetting.
     
  3. Classless Inter-Domain Routing (CIDR): Enables flexible allocation of IP addresses, breaking away from traditional class-based subnetting and allowing for more efficient use of IP addresses.
     
  4. Subnetting by Borrowing Bits: Involves borrowing bits from the host portion of an IP address to create subnets, dividing a larger network into smaller, manageable subnetworks.

Important formulas to determine the subnets

There are several important formulas to determine the subnets:

1. Number of Subnets 

Formula: 2n, where n is the number of borrowed bits.

For example, borrowing 3 bits (n=3) creates 23=8 subnets.
 

2. Number of Hosts per Subnet:

Formula: 2(32−n)−2, where n is the number of borrowed bits.

For example, borrowing 3 bits (n=3) provides 2(32−3)−2=30 usable host addresses.
 

3. Subnet Mask Calculation:

Formula: 256−2subnet bits

For example, for a /27 subnet, 256−227=224, so the subnet mask is 255.255.255.224.
 

4. Total Number of Hosts in a Network:

Formula: 2(32−subnet bits)−2

For example, for a /26 subnet, 2(32−26)−2=62 usable host addresses.
 

5. Subnet Address Calculation:

Formula: subnet_number×block size

For example, if subnetting with a /28 mask, the block size is 16, and the third subnet is 3×16=48.

What is Network Addressing?

Network addressing is the process of assigning unique identifiers to a network device. This device can be your computer, printer, router, or server. It is used to identify these devices on the network and enable communication between them. Without using network addressing, devices will not be able to communicate with each other.

Now, you might be thinking of how this can happen. Let us understand the main concept of the subnet mask in computer networking.

Subnet Mask in Computer Networks

In computer networks, each subnet has unique IPs. The IP address has two parts that are network part and the host part. The network part of an IP address determines the network to which a subnet belongs. The host part of an IP address determines the specific subnets within that network. These both parts have four 8-bit octets. Here the subnet comes into the picture. 

A subnet mask is a 32-bit number. This number is used in determining the network and the host portion of an IP address. This 32-bit number is made by putting 1s at the network portion and 0s at the host portion. 

Now might be thinking, why use a subnet mask in computer networks? Let us discuss this.

Why use Subnet Mask?

The subnet mask is used for many reasons in computer networks. It helps network administrators to divide huge and complex networks into small subnetworks. There are various reasons to use a subnet mask:

  • It helps to improve the network management. As we discussed that it divides the huge network, which helps to manage the traffic easily of small subnetworks.
     
  • It helps to improve the security of the network. By dividing the network, network administrators can easily create small broadcast domains, which can help in preventing network attacks. 
     
  • It helps in improving the scalability of the network. When we have a huge network, it is difficult to manage and scale that network. Due to this, we can easily scale and manage the network.

What is a Subnet Mask Calculator?

There are a number of ways to calculate the subnet mask but many of use subnet mask calculators. It is a tool that helps to calculate the subnet mask. It can also help us to find the subnet address range for a given IP address and the subnet mask prefix length. There are several varieties of subnet mask calculators available:

  • Online subnet mask calculators
     
  • Standalone subnet mask calculators
     
  • Command-line subnet mask calculators
     
  • Network scanner tools with subnet mask calculator
     

Now, let us understand how to calculate a subnet mask from an IP address.

Calculating Subnet Mask from an IP Address

Calculating the subnet mask from an IP address is the crucial part. To do this, we need to identify the network ID and host ID portions of the address. Then we need to determine the number of bits that are used in each of the portions. After getting the number of bits used for the network ID, we can construct the subnet mask. We have to set those network bits to 1 and set the remaining bits to 0.

There are various classes for IP addresses:

  • Class A: This class’s IP address starts with 1 to 127. In this class, the network portion is of 8-bits, i.e., 11111111.00000000.00000000.00000000
     
  • Class B: This class’s IP address starts with 128 to 191. In this class, the network portion is of 16-bits, i.e., 11111111.11111111.00000000.00000000
     
  • Class C: This class’s IP address starts with 192 to 223. In this class, the network portion is of 16-bits, i.e., 11111111.11111111.11111111.00000000
     

There are some steps to calculate the subnet mask from an IP address:

Step 1: First, we need to write the IP address in the binary format. As we discussed that an address has four octets. So, we need to convert each octet of the IP address into its binary equivalent. 
 

Step 2: Then, we have to identify the network ID and host ID portions of the IP address. This is determined by the subnet mask that is being used. 
 

Step 3: Now, we have to count the number of bits that are used for the network ID portion. 
 

Step 4: Now, we have to construct the subnet mask. We can do this by setting the network ID bits to 1s and the host ID bits to 0s. 
 

By following these steps, we will get a subnet mask. This subnet mask will be used to identify the network and host portions of any IP addresses that belong to the same subnet.

Let us understand these steps with the help of an example.

Example to Calculate Subnet Mask

Suppose we have an IP address 180.166.1.0 and a subnet of 255.255.0.0. To calculate the subnet mask of this IP, you can follow these steps:

Step 1: Converting this IP to binary format.

So, our IP address is 180.166.1.0, and this IP address is class B, and we know that class B’s network portion has 16 bits with 1s.

By looking at the octet chart, we can convert this IP to binary format.

8 bit octet chart

So, let us break these octets from our IP address and find a binary equivalent for each one of them.

180= 1+0+1+1+0+1+0+0
166= 1+0+1+0+0+1+1+0
1= 0+0+0+0+0+0+0+1
0= 0+0+0+0+0+0+0+0

So, after merging them, we will see the binary equivalent of the IP address.

10110100.10100110.00000001.00000000 is our IP’s binary equivalent.

 

Step 2: Now, we have to find the network and host ID portions of our IP.

So, 

network id and host id

Step 3: Now, we have to count the number of bits in the network portion.

So, the number of bits is 16.

 

Step 4: Now, we have to replace network ID bits with 1s and host ID bits with 0s.

So, we will get 11111111.11111111.00000000.00000000

So its decimal representation is 255.255.0.0
 

This subnet mask we have used to identify the network and host portions of the IP address that belong to the same subnet as 180.166.1.0.

Also see, Basic Networking Commands  and Subnetting in Computer Networks

Frequently Asked Questions

What is 255.255 255.0 in subnet mask?

255.255.255.0 is a subnet mask that signifies a Class C network with 24 bits allocated for the network and 8 bits for hosts.

Is 255.255 255.0 or 24?

Yes, 255.255.255.0 is equivalent to /24, indicating a subnet with 24 bits allocated for the network and 8 bits for hosts.

Is 255.255 255.255 IP address valid?

Yes, 255.255.255.255 is a valid broadcast address used to broadcast to all devices on the local network segment.

Is 10.0 255.255 8 a valid host address?

No, 10.0.255.255/8 represents a network address. In a /8 subnet, the last three octets are reserved for hosts, making 10.0.255.255 invalid for individual hosts.

Conclusion

In this article, we have discussed the concept of subnet mask. We have also discussed an example based on this concept. We have also discussed its advantages. You can check out our other blogs to enhance your knowledge:

We hope this blog helped you to understand the subnet mask. You can refer to our guided paths on the Coding Ninjas Studio platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews

Previous article
Subnetting in Computer Networks
Next article
Difference between Subnetting and Supernetting
Live masterclass