Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
How to Calculate Number of Host in a Subnet?
Next article icon

How to Calculate Number of Host in a Subnet?

Last Updated : 16 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Subnetting is a critical task in a network setup involving dividing a larger network into smaller, manageable subnet segments. Calculating the number of hosts that can fit into each subnet is essential for efficient IP address usage and network optimization. This calculation not only ensures efficient use of IP addresses but also enhances network performance and security.

In this article, you will get to know about the methods and formulas needed to determine host capacity within a subnet, the importance of network and broadcast addresses, and practical examples.

Subnetting and IP Addresses

Subnetting and IP addressing are fundamental aspects of network design, crucial for efficient network management and optimization.

  • An IP address is a unique string of numbers separated by periods (IPv4) or colons (IPv6) that identifies each computer using the Internet Protocol to communicate over a network. It allows devices to locate and communicate with each other on a network.
  • An IP address, generally in IPv4 or IPv6 format, serves as a unique identifier for devices on a network, with IPv4 consisting of 32 bits divided into four octets and IPv6 offering a more expansive 128-bit format.
  • Subnetting is the process of dividing a larger network into smaller segments known as subnets. It enhances network management by optimizing IP address usage and improving network performance and security.
  • Subnet Mask is utilized to differentiate between the network and host portions of an IP address. It determines how the IP address is divided between network and host, affecting how network traffic is routed.
  • Subnetting reduces broadcast domains, limiting the spread of broadcast traffic within a network which improves network performance.
  • Allows for better control over network traffic flow, enhancing security by isolating broadcast traffic within specific subnets.
  • Improves overall network efficiency through targeted and efficient use of IP addresses.

Practical Application:

  • Adjusting the Subnet Mask: For example, changing from 255.255.255.0 to 255.255.255.192 allows administrators to create smaller, more manageable networks.
  • Host Capacity Calculation: Uses formulas like 2(32-n)−2 to calculate the number of usable hosts per subnet, where n represents the number of bits designated for the network and subnet parts.
calculating_number_of_host_in_a_subnet
Calculating Subnets and Hosts

Explanation:

  • Subnet Mask: This is the mask used to determine what portion of an IP address refers to the network and what part refers to the host.
  • Subnet Notation: This is the CIDR (Classless Inter-Domain Routing) notation that also indicates the subnet mask.
  • Total Subnets: The number of distinct networks that can be created with the given subnet mask.
  • Usable Hosts per Subnet: The number of devices that can be assigned IP addresses within each subnet, excluding the network and broadcast addresses.

We use fundamental networking formulas that are based on the number of bits allocated for network and host parts in the subnet mask. Here’s a breakdown of the calculations:

Basic Formulas:

  1. Number of Usable Hosts: Usable Hosts = 2h - 2, Where h is the number of bits used for hosts in the subnet mask. The subtraction of 2 accounts for the network address and the broadcast address, which cannot be assigned to hosts.
  2. Number of Subnets: Total Subnets = 2s Where s is the number of bits borrowed from the original host part to create more network bits in subnetting.

Example Calculation for a /25 Subnet Mask:

  • Subnet Mask: 255.255.255.128 or /25
  • CIDR Notation: /25 indicates that 25 bits are used for the network part.

Calculating Usable Hosts:

  • Total bits for IPv4 = 32
  • Network bits = 25
  • Host bits h = 32 - 25 = 7
  • Usable Hosts = 27−2 = 126

Calculating Total Subnets: Assuming we are subnetting within a larger block (e.g., a /24 block):

  • Bits borrowed for subnetting s = 25 - 24 (original block) = 1
  • Total Subnets = 21 = 2

Applying the Formulas to Different Masks:

The same process applies to other subnet masks, adjusting the number of host bits (h) and the number of borrowed bits (s) accordingly:

  • /26 Subnet Mask:
    • Network bits = 26, Host bits h = 6
    • Usable Hosts = 26−2 = 62
    • Assuming subnetting within a /24, s = 2, Total Subnets = 22 = 4
  • /27 Subnet Mask:
    • Network bits = 27, Host bits h = 5
    • Usable Hosts = 25−2 = 30
    • Assuming subnetting within a /24, s = 3, Total Subnets = 23 = 8

These calculations are essential for network engineers and administrators to efficiently plan and allocate IP addresses within a network, ensuring optimal utilization and avoiding address exhaustion.

Steps to Find the Number of Computers Connected in the Given IP Address 

1) Identify the Class of the IP address

To find the number of computers connected in the network first we need to identify the class of the IP address, there are 5 classes of IP addresses they are A, B, C, D, and E.
Each IPv4 address consists of 32 bits, divided into 4 octets, 1 octet = 8 bits. Look at the first octet to find the class of the given IP address. The range of each class is given in the following table.

classes_of_ip_address
classes of IP Address

Example: If the IP address given is 64.19.23.0 then the first octet is 64 which is in the range of 0 to 127, so the given IP address belongs to class A.

2) Finding the Network IP address 

Finding the network IP address involves determining the portion of an IP address that identifies the specific network to which a device belongs. This can be accomplished by using the subnet mask along with the device's IP address. Here's how you can find the network IP address:

  • Understand IP Address and Subnet Mask: Every device on a network has an IP address and a corresponding subnet mask. The subnet mask helps in identifying how the IP address is divided into network and host parts.
  • Binary Conversion: Convert both the IP address and the subnet mask into binary. An IP address is composed of four octets (e.g., 192.168.1.15), as is the subnet mask (e.g., 255.255.255.0).
  • Perform a Binary AND Operation: Perform a bitwise AND operation between the binary form of the IP address and the subnet mask. This operation compares corresponding bits of the IP address and the subnet mask and applies the logical AND operation, which results in the network portion.

For example: If the IP address is 192.168.1.10 (11000000.10101000.00000001.00001010 in binary) and the subnet mask is 255.255.255.0 (11111111.11111111.11111111.00000000 in binary)

Then result of the AND operation would be 11000000.10101000.00000001.00000000, which corresponds to 192.168.1.0.

  • Convert Back to Decimal: Convert the result of the AND operation back to decimal format to get the network address. This is the part of the IP address that identifies the network to which the device is connected.
  • Verification: You can use various network tools or command-line utilities like ipconfig (on Windows) or ifconfig (on Unix/Linux) to display the IP address, subnet mask, and network address for verification.

3) Finding the number of hosts or number of computers connected to that network 

The class it belongs will tell the range of hosts that can connect to that network, it's given in the below table. 

ClassesNumber of networks possibleNumber of hosts possible in 1 networkNumber of usable hosts in 1 network
A126224224 - 2
B163846553665534
C221256254
Dno networks no hosts-
Eno networksno hosts-

[Note: Class D is reserved for Multicasting, group email/ broadcast, possible IP address in class D are 228 

Class E is reserved for experimental and research / military purposes, possible IP addresses in class E is 228]

In simple words, the Number of hosts in any network can be calculated with the formula = 2x- 2, where x is the number of host ID bits in the IP address. 

Why do we subtract 2?

Because the first and last addresses are not used for any hosts because the first IP is used to represent the whole network ID while the last IP is used as the broadcast address.

Example: The given IP address is 192.168.254.1  it belongs to class C, when we perform AND operation on IP address with a default mask of class C, we get the network IP address as 192.168.254.0 this is the IP address used to represent the whole network and the broadcast address will be the last IP address of this network which is 192.168.254.255

Example 1: Find the Class, network IP address, number of hosts (computers), and broadcast address of 9.1.5.31 

Answer: Finding the Class to which the given IP address belongs to 
              The first octet has a value of 9 which is in the range of 0 to 127 so the given IP address belongs to  Class A.

Finding the Network IP address
The default mask for class A as given in the table is 255.0.0.0

Perform the AND operation to get the network IP address 
              9.1.5.31     => 00001001.00000001.00000101.00011111
              255.0.0.0   => 11111111.00000000.00000000.00000000
              00001001.00000000.00000000.00000000 => 9.0.0.0
              
              IP address =  9.1.5.31, Network address= 9.0.0.0
              So, Network ID bits= 8 (first octet), Host ID bits = 24 (Last three octets)

              The network IP address of the given IP address is 9.0.0.0 
              The number of hosts in each network is 224- 2 
              The broadcast IP address is 9.255.255.255

Example 2: Find the Class, network IP address, number of hosts (computers), and broadcast address of 201.20.30.40

Answer: Finding the Class to which the given IP address belongs to
              The first octet has a value of 201 which is in the range of 192 to 223 so the given IP address belongs to Class C.
              Finding the Network IP address
              The default mask for class C as given in the table is 255.255.255.0

              Perform the AND operation to get the network IP address
              201.20.30.40     => 11001001.00010100.00011110.00101000
              255.255.255.0   => 11111111.11111111.11111111.00000000
                                             11001001.00010100.00011110.00000000 => 201.20.30.0
              IP address =  201.20.30.40, Network address= 201.20.30.0
              So, Network ID bits= 24 (first three octets), Host ID bits= 8 (Last octet)

              The network IP address of the given IP address is 201.20.30.0
              The number of hosts in each network is 28- 2= 254
              The broadcast IP address is 201.20.30.255


Next Article
How to Calculate Number of Host in a Subnet?

G

gurukiranx
Improve
Article Tags :
  • Computer Subject
  • Computer Networks

Similar Reads

    Find the Number of Cliques in a Graph
    In graph theory, a clique is a subset of vertices of an undirected graph such that every two distinct vertices in the clique are adjacent, that is, they are connected by an edge of the graph. The number of cliques in a graph is the total number of cliques that can be found in the graph. The Mathemat
    15 min read
    How to Find the Subnet Mask of Your Computer?
    Like your Device IP Address, a Subnet Mask on Computer helps identify devices on a Local Network. To Connect To Network and use internet resources, you must Find Subnet Mask on Computer.Getting Details of Subnet Mask is necessary all over the Operating Systems from Windows OS to Linux and MacOS. Thi
    5 min read
    Subnet Mask Calculator – Accurate IPv4 & IPv6 Network Calculation Tool
    A Subnet Mask Calculator is an essential tool for network administrators, IT professionals, and network engineers to perform crucial IP address subnetting tasks. It simplifies complex calculations by helping users compute IP ranges, broadcast addresses, subnet masks, CIDR notations, and usable host
    6 min read
    C Program to find IP Address, Subnet Mask & Default Gateway
    Terminology IP Address : An IP address, short for Internet Protocol address, is an identifying number for a piece of network hardware. Having an IP address allows a device to communicate with other devices over an IP-based network like the internet. Subnet mask: A subnet mask is a 32-bit number used
    2 min read
    IP Custom Subnet Masks
    To efficiently manage networks, it's important to understand IP Custom Subnet Masks. These subnet masks allow network administrators to design and manage subnets in a flexible and tailored way, accommodating specific needs of different network sizes. Custom subnetting is especially useful when deali
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences