56
| Appendix A
Before we get too deep into binary numbers, host and network address parts, you should be aware that you can safely treat the
subnet mask as just another parameter to set when you configure an IP address. No deep understanding is required to find out
what the local subnet mask is and to configure it for your IP device.
We should also mention that one of the most commonly used subnet masks is 255.255.255.0. Read on to find out why.
Network and Host IDs
To deal with subnets, IP treats the IP address as consisting of two separate IDs: A network ID and a host ID. The
network ID identifies the subnet while the host ID identifies the actual device on the subnet. If two devices have
addresses with the same network IDs, then they are on the same subnet and are able to exchange data directly. If
not, then they are on different subnets. Any data exchange between them would have to go through a router.
The protocol needs to split the IP address into the network and host parts so that it knows where to send the data
packets. It accomplishes this splitting by using the subnet mask.
A subnet mask is a 32-bit number and is commonly written out in dotted decimal notation, just like an IP address. If
you write it out as a binary number, you will notice that it consists of a string of ones followed by a string of zeroes.
The ones indicate (they “mask”) the network portion of the IP address while the zeroes indicate the host portion.
An example might explain this better. Using the same IP address as above, 192.168.1.65 with a subnet mask of
255.255.255.0 we found out that the network ID is 192.168.1.0, while the host ID is 65. Here is how we did it:
Step 1:
Convert the mask to binary and we get 11111111 11111111 11111111 00000000
Step 2:
Convert the IP address to binary as well:
Step 3
: For each 1 in the subnet mask, keep the corresponding IP address bit as is, and for each 0, convert the
corresponding bit to 0. We end up with:
11000000 10101000 00000001 00000000 (or 192.168.1.0)
This is the network ID part of the IP address.
Step 4: Now, we invert the mask. For each 0 in the subnet mask, keep the corresponding IP address bit as is, and for
each 1, set the corresponding bit to zero. We get:
00000000 00000000 00000000 01000001 (or 65)
As you might know, computers love ones and zeroes and built-in binary operations make it a breeze to perform the
network/host splitting. In a language like C or C++, the splitting would look like this:
Network id = ip_address & mask;
host_id = ip_address &(~mask);