Subnetting and Subnet Masks Explained

classA-subnetWhat is Subnetting ?-Subnetting is the process of diving a network into small networks and is a common task on IPV4 networks.

Before we discuss how to implement it it is useful to understand why and when we need to do it and to do that we are first going to work through a simple analogy to illustrate the problem subnetting solves

Subnetting Analogy

As an analogy imagine a school and we need to split it into class rooms.

But why split it into class rooms? The answer is to stop classes interfering with one another.

Now each classroom has a desk with a computer and we have been tasked with creating a labeling system for the computers.

Now say we have 30 classrooms each with a maximum of 30 students and computers.

If we assign numbers to our classrooms and computers then we could have for example:

computer 11, classroom 24

We need two digits for the classroom which would allow for a maximum of 100 classrooms (0-99).

We need two digits for the computer which would allow for a maximum of 100 computers (0-99).

If we also say that classroom numbers 0 and 99 and computer numbers 0 and 99 were reserved and not allowed to be assigned then we now have a maximum of 98 classrooms and 98 computers which is enough for own requirements.

So lets create our label we could use the following scheme:

  • computer 11, classroom 24
  • 24-11
  • 11-24
  • 2411
  • etc

There are many possible permutations we just need to pick one and tell every one about out labeling scheme.

Lets assume we go for 2411 where 24 is the classroom and 11 is the computer.

So now when we see the following 0223 we now know that this refers to classroom 2 and computer 23.

This we do easily in our heads once we know the labelling scheme.

We could even make it easier for ourselves by creating a paper mask that we put over the label that would reveal the classroom.number-mask-illustration

IP Addresses and Subnetting

Just like in our classroom example an IP address is split into two components a network component and a node component.

So the address 10.0.2.1 is split into Network plus Node.

So is the network number  10, or 10.2 or 10.0.2 ?

In early IPv4 networks address classes were used to identify the number of bytes allocated to the network component.

The main classes were class A,B,C. With the allocation as follows:

Class A    network,node,node,node
Class B    network,network,node,node
Class C    network,network,network,node

To determine the class you needed to examine the most significant byte (far left).

  • 0-127 Class A
  • 128-191 Class B
  • 192-ccc Class C

IP Subnetting- Problems with Large Networks

All modern networks use the Ethernet data link protocol.

Ethernet uses a shared media and is negatively effected when a large number of nodes are connected to the same media.

This is just the same as having too many kids in the same classroom.

You can equate a network and node address to our classroom and desk number

What would happen if you had a classroom with 100’s of desks i.e. hundreds of pupils?

So even though a Class A address can accommodate thousands of nodes it it totally impractical to put this many nodes on a single network.

The solution to the problem was to split the network into small networks called sub networks or subnets.

Take for example a class A address which uses 1 byte for the network ID and 3 bytes for the Node ID. Written

Net.Node.Node.Node

It is important to understand that the network part of the address is only used for routing IP packets on the public internet.

Once the packet enters the private network then the Node address is used and the public Network address is not used.

Now a network administrator can interpret the node address any way they want, and so it is possible to split the node address into subnet and Node. So we could have

Net.Subnet.Node.Node or Net.Subnet.Subnet.Node.

The technique used to create subnets is to use a Mask.

The mask effectively hides the Node component and leaves the network and sub network components.

If the IP address was printed on paper we could hide the last byte by placing a paper mask over the number with three holes.

classA-subnet

Note: if your binary is a little rusty see the binary numbers tutorial

To do this on a computer we use a number which we then logically AND with with IP address.

Here is the logic table for AND

1 and 1 =1
1 and 0 =0
0 and 1 =0
0 and 0 =0

So our mask to hide the last byte is 255.255.255.0   As 0 and Anything is always 0

The best way to see this is to write the numbers out in binary

000001011.00000001.00000001.00010101 = 11.1.1.21
11111111.11111111.11111111.00000000 =255.255.255.0

and then do a logical AND

the Network address is 11.1.1.0 which corresponds to network 11 subnet 1.1

The Subnet mask determines how the IPv4 address is split.

Th example above used a class A IP address we can do the same with a class B address.

Net.Net.Node.Node —->Net.Net.Subnet.Node Using Mask 255.255.255.0

Subnetting on a byte boundary is the easiest to do and understand but we can also subnet on non byte boundaries.

Worked Examples

1- You have been allocated a class A network address of 29.0.0.0. You need to create at least 20 networks and each network will support a maximum of 160 hosts. Would the following two subnet masks Work?

255.255.0.0 and or 255.255.255.0

Yes both would work.

Mask 255.255.0.0 has 8 bits for the subnet and 16 bits for the host

8 bits would accommodate 28=256 subnets

16 bits would accommodate 216= over 64000 hosts

Mask 255.255.255.0 has 16 bits for the subnet and 8 bits of the host.

Have possible 28 -2 hosts =254 which is enough.

2. – You have been allocated a class B network address of 135.1.0.0 and and need to create 4 subnets each with around 200 hosts what is the easiest mask to use to satisfy the criteria?

Easiest is to sub net on a byte boundary which would mean a subnet mask of 255.255.255.0

This would allocate 8 bits for the subnet and 8 bits for the host.

We need to accommodate around 200 hosts which requires 8 bits which we have.

We need 4 subnets which requires 4 bits and we have 8 bits. So we have more than enough.

Classless Inter-Domain Routing (CIDR)

Classless Inter-Domain Routing was introduced in 1993 to replace the classful network design.

Instead of allocating network addresses using address classes based on 8 bit groups it uses variable length subnet masking.
It also introduced a new method of denoting network masks.

Example:
A class C network would have a subnet mask of 255.255.255.0 which means that 24 bits are used for the network.

In CIDR notation this is designated by a /24 following the IP address. So:

IP address 192.168.1.168 subnet mask 255.255.255.0 is written as:  192.168.1.168/24 in CIDR notation.

Table: CIDR and Subnet Examples

Address Class No of Network Bits No of Host Bits Subnet mask CIDR notation
A 8 24 255.0.0.0 /8
A 9 23 255.128.0.0 /9
A 12 20 255.240.0.0 /12
A 14 18 255.252.0.0 /14
B 16 16 255.255.0.0 /16
B 17 15 255.255.128.0 /17
B 20 12 255.255.240.0 /20
B 22 10 255.255.252.0 /22
C 24 8 255.255.255.0 /24
C 25 7 255.255.255.128 /25
C 28 4 255.255.255.240 /28
C 30 2 255.255.255.252 /30

Worked Examples

  1. Write  the IP address 222.1.1.20 mask 255.255.255.192 in CIDR notation

Decimal 192 =11000000 binary which means that 2 bits of this octet are used for the subnet. Now add the 24 bits 255.255.255 and we have 26 bits. So we write:

222.1.1.20/26

2. Write is the IP address 135.1.1.25 mask 255.255. 248.0 in CIDR notation

Decimal 248 =11111000 binary which means that 5 bits of this octet are used for the subnet. Now add the 16 bits 255.255. and we have 21 bits. So we write:

135..1.1.25/21

Simple Subnetting Exercise Questions

The best way to learn to do subnetting is to try some examples. We will look a some common subnetting problems that arise when creating networks.

1 –  You have been allocated a class C network address of 201.1.1.0 how may hosts can you have?

2- You have been allocated a class A network address of 21.0.0.0. You need create at least 10 networks and each network will support a maximum of 100 hosts. Would the following two subnet masks Work.

255.255.0.0 and or 255.255.255.0

3 – You have been allocated a Class B network address of 129.1.0.0. You have subnetted it using the subnet mask 255.255.255.0 How many networks can you Have and how many hosts can you place on each network?

Answers

A1– 256-2= 254  – Why? host addresses of all 0’s and all 1’s are not allowed.

A2- Yes you only need 8 bits for 100 hosts and both subnet masks would give you that.

A subnet mask of 255.255.255.0 would give you lots of networks (2 16) and 254 hosts.

A subnet of 255.255.0.0 would give you lots of hosts (approx 216) and 256 networks.

A3 – the network has 8 bits and so does the node component. This means that you have have 28 =256 networks and 256 -2 = 254 hosts

Subnetting Class C Addresses

So far we have been subnetting on a byte boundary using class A and B addresses.

Now we are going to look at how we subnet on a non-byte boundary using a Class C Address.

Exercise- You have been allocated a class C network address of  195.1.1.0.

You need to create 5 sub networks each network has a maximum of 10 hosts.

Now our current subnet mask is 255.255.255.0

We can only use the first 8 bits for out subnets as these 8 bits have been allocated as host addresses.

So for 10 hosts we need 4 bits (16-2 =14 hosts)

for 5 subnets we need 3 bits (8 networks possible)

possible masks:

11100000=224 (Note: 3 bits for sub networks)

11110000=240 (Note: 4 bits for sub networks)

So Subnet masks of 255.255.255.224 and 255.255.255.240 would both work.

In CIDR notation we have 195.1.1.0/27 and 195.1.1.0/28

Subnet Table and Calculator

Below is a simple subnet table that makes it easier for calculating subnets.

subnetmask-Table

There are also many online subnet calculators available like this one



Subnet Worked Examples

Subnetting Worked Examples and Exercises

Subnet Quizes

Test you knowledge using these online quizes

Related Tutorials and Resources:

Save

Please rate? And use Comments to let me know more

40 comments

  1. Thanks, very useful article!
    One question though… With these exercises above the answer of A-1 starts with… 255-2= 254

    Is it me, or does this sound weird? 😉

  2. “We need 4 subnets which requires 4 bits and we have 8 bits. So we have more than enough.”
    Isn’t 4 subnets requires 2 bits?

    1. 4 subnets require 3 bits as 2 are always unused the all0 and all 1 s so with 4 bits
      00 =unused
      01=ok
      10=ok
      11=unused
      rgds
      steve

  3. Sir if we want to make 4 subnets of class c ipv4 and one of it should have 75 host bit the limit is of 62 host per subnet , then what should we have to do to find its subnetmask?? Please anyone respond

    1. It Looks like a VLSM question which I don’t cover on the site because I never did VLSM in real life as there wasn’t the need when I was doing it.

      Rgds
      Steve

    2. Hi Labeeb, In that case, you would need to take 2 IPs which further can be devided in to 4 (/25) subnets. For ex:
      1. 192.168.0. 0/25 (0.1 to 0.126)
      2. 192.168.0.128./25 (0.129 to 0.254)
      3. 192.168.1.0/25 (1.1 to 1.126)
      4. 192.168.1.128/25 (1.129 to 1.254)

  4. Hi Steve,
    Thanks for your this explanation. Very useful.
    Just a question.
    Can we not apply the mask 255.0.0.0 to IP 29.0.0.0 ?

  5. Thank you for this awesome explanation. I watched numerous YouTube tutorials, read a few books, but your explanation is so straightforward and easy to understand it immediately clicked! 100x times thanks!

  6. Under heading “IP Subnetting- Problems with Large Networks”
    it says on 6th line that :
    “So even though a Class A address can accommodate thousands of nodes, it is totally impractical to put this many nodes on a single network.”

    why is it impractical?

    1. It is because the data link protocol is Ethernet and this was designed to use coax cable which is a shared media. UTP still uses the shared media design. in this configuration as you add more nodes the collision rate increases and the speed decreases until eventually it stops.
      There appears to be a 1024 node limit but most ethernet networks aremuch smaller see here
      https://learningnetwork.cisco.com/thread/11572

  7. wow, just wow, i have spent 3 days in classes trying to understand all this stuff and this page has just explained everything to me in about an hour! God bless you Steve!

  8. I thank you a lot Steve with this great effort. You made it easy for us.
    Many thanks.
    Mohamud

  9. Yeah nicely done. Much appreciated. Managed to grasp CIDR – even after a couple pilsner. 😉 Now I’m a cider fan too.

  10. Really helped me to understand the concept of subnet masks and how they work. Thank you!

  11. “Well in early IPv4 networks address classes were used to identify the number of bytes allocated to the network component.” Please note that a comma is needed here and depending on where you will place it the provided information will be entirely different.

    On the other hand, great explanation, I’m one step forward in understanding netmask

  12. In the process of simplifying, you over complicated the whole process.
    Content is really good. but explanation is over complicated

  13. For decades I roamed the earth with the horrible awareness that I don’t grasp subnet masks and CIDR notations … 20 mins on this page and now it all seems so clear. Thanks!

  14. Hi Steve
    Thank you for this explanation, it is good as always. It was one part that I didn’t quite understand though. How do i know how many networks and hosts I can place on each network?

    1. Hi
      If you take a class c network as an example the starting subnet mask is 255.255.255.0
      The lower 8 bits you can use for subnetting.
      if you use 4 bits for the sub network and 4 for hosts you get a subnet mask of
      255.255.255.240
      128+64+32+16=240
      the network uses the high order bits
      anyway 4 are for the network. With 4 bits you can have 16 values 0000 to 1111
      now 0000 and 1111 are normally not allowed for network or host addresses therefore you can have 14 subnets and 14 hosts.
      Hope that makes sense
      You might also want to look at the worked examples
      http://www.steves-internet-guide.com/subnetting-worked-examples/

      1. Do you have to subtract 2 addresses from the network addresses as well as the host addresses? In the main post, A3 for the exercise questions, it says that you have 256 networks and 254 hosts. You’re only subtracting the all 1 and all 0 addresses from the hosts. What am I missing?

        1. Yes That is correct. It wasn’t previously possible to use the all1’s or all0s for subnets or hosts with classfull addressing. That situation changed with classless addressing. However use of all1s and all 0s is discouraged. See this very good cisco discussion on the subject
          https://www.cisco.com/c/en/us/support/docs/ip/dynamic-address-allocation-resolution/13711-40.html

          If you are worried about exam questions then you might find they state if all 1s and all 0s can be used.
          Personally I would avoid all 1’s and all 0s
          rgds
          steve

  15. I thought I could never understand networking concepts. Your article is making me think, if things are explained in a way as you have done, I can and everyone can understand anything. Thanks Steve!!

  16. your blog is very helpful steve,u made me understand subnetting within 30 minutes yet i have been readin gjunks for almost 8 years without gasping anything

Comments are closed.