Subnetting creates additional network IDs at the expense of host IDs and can be used with either A, B or C class addresses. If you look at Table 1, you will notice that a class B address uses 14 bits for network addressing and 16 bits for host addressing. By simply reassigning one
of the host bits to a network bit, you would double the number of available network addresses but halve the number of host addresses. Carrying the argument further, move eight of the host bits (actually the complete third byte) to the network side. The result is 22 bits for network addressing and eight bits for host addressing which is quite similar to a class C address. These additional network addresses are called subnets and not networks because to the Internet, the original address is still a class B network address but locally the class B network address can be broken down to manageable subnets that function as actual network addresses. Why use subnets? Subnets are interconnected using routers, and routers improve network performance by reducing traffic and
minimizing disruption due to broadcast messages. Large networks become more manageable when subnets are deployed.

To create subnets you need a subnet mask
that defines which bits will be used to create the new network address out of the
32-bit IP addresses. By “ANDing” the 32-bit IP address with a 32-bit mask, we create a 32-IP address that represents <netid, subnetid> becoming our new network address. What do these masks look like? If we start with a basic class A address and do not define any subnets, the mask would look like 255.0.0.0 which is called a natural or default mask. Only those bits that are set as a 1 will be considered when defining a network address. In this case, all the bits in the first byte of the IP address will be considered. The natural mask for a class B address is 255.255.0.0 and for a class C address it is 255.255.255.0. In order to create more network addresses (subnets) we need to move the mask bits to the right (changing 0 bits into 1s) in order to convert host bits into network bits. The best way to understand the concept is to use an example.

Assume we begin with IP address
165.10.0.0. From Figure 1 we know that this is a class B address with a network address of 165.10 with the capability of assigning up to 65,534 hosts. We do not want 65,534 hosts on one network but would like to have up to 500 hosts on each subnet. In order to have 500 hosts on one subnet, we need to have 9 bits of host addressing. Currently, we have 16 bits of host addressing since we possess a class B address. That means that we can reassign 7 of those bits to signify subnet bits. Therefore, the subnet mask would be 255.255.254.0. In binary it would be:

11111111.11111111.11111110.00000000

The natural mask for a class B address is 255.255.0.0 so in order to create subnets we moved mask bits to the right in order to convert more host bits to network bits. It must be remembered that these mask bits must be contiguous from the left. For example, the above mask allows up to 510 host assignments. Remember that we cannot use either an all 0s or all 1s host address. The
next jump would be to allow up to 1022 host addresses. What would be the subnet mask? It would be 255.255.252.0. The 1s are still contiguous from the left. This approach creates many subnets, but it is recommended that neither an all 0s nor all 1s subnet be used. This could cause a problem on some networks. How many mask bits can you have?
You need to have some hosts on a network and two host addresses are unusable so the maximum number of mask bits is 30, leaving two valid host addresses.