The subnet mask enables us to determine what is the network ID for a given IPv4 address. We learned there are two components in a 32-bit IPv4 address. The two components are
- Network ID
- Host ID
From the 32 bit IPv4 address a portion of bits are reserved for defining the network ID. This is the network portion. Then the remaining portion of bits are used to define the host IDs. This portion of bits are known as the host portion. For all hosts in the same network the network portion bit pattern is identical among all hosts in the same network. However, the host portion bit pattern MUST be unique to each host in the same network.
Figure The structure of an IPv4 address
Let’s look at 192.168.10.10 /24 and 192.168.10.254 /24. These are two IPv4 addresses that are in the same network. The /24 is the subnet mask. What the /24 is expressing is from left to right the first 24 bits are reserved for the network portion. This method of representing the subnet mask is known as the slash notation or prefix length. The /24 can also be expressed as 255.255.255.0. This is the dotted decimal subnet mask. The 192.168.10.10 /24 is the same as 192.168.10.10 255.255.255.0.
As seen in the figures below, the address bits that have a corresponding mask bit (The row labelled “Binary Subnet Mask”) set to 1 represent the network portion. If you count the number of 1 bits it is equal to 24. That’s what was expressed by /24.
Any address bits that have a corresponding mask bit set to 0 represent the host portion. The Host portion consists the range of bits we can use to assign a Host ID.
We can observe that both the IPv4 addresses are having an identical network portion bit pattern. In other words they have the same network ID. The dashed line marks the boundary between where the network portion ends and the host portion begins.
However, when we examine the host portions they are not identical. This is to say they are two different host IDs. The conclusion is these are two different IPv4 addresses in the same network.
Figure Comparing the IPv4 address and the Subnet Mask of 192.168.10.254
Figure Comparing the IPv4 address and the Subnet of 192.168.10.10
As seen above, given a particular IPv4 address, the means by which we identify the Network ID and Host ID is looking at the subnet mask. Also, the subnet mask instructs a TCP/IP host which portion of the 32 bits identify the network portion and which identifies the host.
How to determine the network ID using subnet mask and logical AND
Once we have the binary representation of the IPv4 address and the subnet mask, it is fairly simple to figure out the Network ID.
Moving from left to right logically AND the IPv4 address bit with the corresponding subnet mask bit, bit by bit.
The Logical AND
The logical AND works as follows.
1 AND 1 = 1
0 AND 1 = 0
0 AND 0 = 0
1 AND 0 = 0
This is very simple. Just remember 1 AND 1 produces 1 and all other combinations produce 0.
To illustrate how AND is used to discover a network address IPv4 address 192.168.10.254 and subnet mask 255.255.255.0 from our earlier example.
Figure AND operations & Resulting Network Address
The resulting network ID is 192.168.10.0 255.255.255.0. Therefore, 192.168.10.254 is on network 192.168.10.0 255.255.255.0.
Sample of various subnet masks
|Decimal Subnet Mask||32-bit Binary Address||Prefix Length|
Table Subnet mask in Dotted decimal, binary and prefix length