Every host or device on a TCP/IP network MUST have an IP address assigned in order to communicate with other devices. An IP address consists of a network part and a host part. Think about the Network part as a multi dueling Building Address number, and the Host part as your apartment number inside this building. For example, building address “Building XYZ” is the network IP address part, and “Apartment number 2” is the host address part.
For example IP address 10.0.0.2 which identifies a single host, contains the network part 10.0.0 and the host part 2. Now, how do devices on the network know which portion is the network part and which is the host part of their assigned IP address? They know this information using the “Subnet Mask”. Every host on a TCP/IP network is configured with an IP address AND a subnet mask. The subnet mask is the one which identifies the Network Part portion of the IP address assigned to the host. For our example above, the host with IP 10.0.0.2 is assigned also a subnet mask 255.255.255.0. If you do a logical AND operation between the IP address and the subnet mask, you will find the Network potion of the address:
10.0.0.2 AND 255.255.255.0 = 10.0.0.0 (The network part is 10.0.0 and the remaining part is the host part. i.e 2 ).
Let’s see a diagram below:
From the picture above, Host A and Host B belong to the same local subnetwork (10.0.0.0/24) and are connected to the same switch together with a router interface. The router interface has also an IP address 10.0.0.254 with the same subnet mask 255.255.255.0 as the two Hosts.
Also, two other hosts (Host C, Host D) belong to another subnetwork (10.1.1.0/24) together with the second interface of the router which has address 10.1.1.254.
Each host has also a default gateway assigned (in addition to IP address and Subnet Mask). Hosts A and B must be configured with a default gateway address of the router interface which is 10.0.0.254. Similarly, Hosts C and D must be configured with a default gateway address of their router which is 10.1.1.254.
How Hosts use the Subnet Mask
When a host wants to communicate with another host, it uses its subnet mask to compare the network portion of its local network IP address with the destination network address of the packet to be sent. Before an end system can send a packet to its destination, it must first determine whether the destination address is on the local network. This is done by comparing the bits in the destination address with the network bits of its own IP address. For example, if Host A wants to send a packet to Host B, it will take the destination address 10.0.0.2 (Host B) and perform an AND operation with its subnet mask. The result will be 10.0.0.0 which will tell Host A that the destination address belongs to the same subnetwork as itself. Therefore it will NOT send the packet to the default gateway (router). Rather, Host A will perform an ARP request (Address Resolution Protocol) to find out the destination MAC address of Host B (ARP protocol maps an IP address with a MAC address). Therefore, Host A will send the packet directly to Host B through the switch without going through the router.
Now, if Host A wants to send a packet to Host C, it will take the destination address 10.1.1.1 of Host C and perform an AND operation with its subnet mask. The result will be
10.1.1.1 AND 255.255.255.0 = 10.1.1.0
which will tell Host A that the destination address has a different network portion than itself (10.1.1). Therefore Host A will have to send the packet to its default gateway (router address 10.0.0.254) in order to reach Host C on the other side of the router.