Every IPv4 address has three components: network, host and subnet mask.
The subnet mask marks the boundary between the network and the host bits in an IP address. It also helps in differentiating the network address (the first address) and the directed broadcast address (the last address) in the IP network number.
The subnet mask is a 32-bit address which has the following properties:
1. The binary-0 represents the host and the binary-1 represents the network. For example 192.168.1.0/24 has a subnet mask 11111111.11111111.11111111.00000000 (in binary) or 255.255.255.0 (in decimal). And hence the first three octets (1s) represent the network and the last octet (0s) represents the host. In other words all the nodes in the network 192.168.1.0/24 segment will have the same network part (192.168.1.*) and the host part of their address varies only in the last octet (192.168.1.1-254).
2. The 0s and 1s in the subnet mask must be contiguous both within the octet and across all the four octets. Study the following examples:
10000000.11111111.11111111.11111111 (184.108.40.206) — invalid
11111111.11111111.11111111.10000000 (255.255.255.128) — valid
11111111.11111111.00101000.00000000 (255.255.38.0) — invalid
11111111.11111111.00000000.00000000 (255.255.0.0) — valid
Subnet masks can be represented in 4 ways. Here a list of those ways demonstrating a Class C address.
The dotted-decimal notation and the number of network bits are the most common representations of the subnet mask.
Subnet mask values : Given the fact that values must have all 1s contiguous and 0s contiguous, following are the valid decimal numbers for subnet masks within an octet.
Below is the default subnet masks for Class A, B and C along with the number of network and hosts they encompass.
The disadvantage of Classful addressing with default subnet masks is poor address efficiency. With Class A addressing for example a network segment can have up to 16,777,214 hosts; Class B 65,534. A typical IP network segment starts to experience the broadcast problem when the number of nodes hits 200 to 500. Assigning a Class B address to a network segment of 500 nodes would waste around 65,000 addresses. You can assign two class C addresses, but you end up with two networks. To solve this problem subnetting was introduced (RFC 950).
With subnetting the higher-order host bits can be borrowed and used along with network bits. As a consequence the network gets segmented into multiple subnets within the network defined by the default subnet mask. One problem with subnetting is as more subnets are created you’ll loose two addresses per subnet (network and directed broadcast address). However, the improvement in address efficiency outweighs the number of IP addresses that need to be reserved for network and directed broadcast addresses.
Subnet mask itself means nothing without the context of the IP address associated with it. For example, most people would assume that when you see a subnet mask of 255.255.255.0, you are dealing with a Class C network. However, remember that you can perform subnetting on any class address: A, B and C. So a subnet mask of 255.255.255.0 can also be used for Class A and B networks. Therefore, the IP address and subnet mask have a symbiotic relationship.
Below is the table of valid subnet mask values. Note that the Class A subnetting starts from /9 to /30; Class B from /17 to /30 and Class C from /25 to /30.
A tutor named Dan explains nicely the concept of subnetting.