To truly understand how to derive IP subnet masks and apply them to addresses, you must understand binary numbers and how to convert them to decimal numbers. Let's start with something that we're all pretty comfortable with--namely, decimal (base 10) numbers. Each digit in a decimal number stands for a different power of 10. You interpret the number 7836, for example, as follows:
7*103 + 8*102 + 3*101 + 6*100 =
7*1000 + 8*100 + 3*10 + 6*1 =
7000 + 800 + 30 + 6 =
7836
You interpret binary (base 2) numbers the same way as you do decimal numbers, except that each column of a binary number represents a different power of 2 rather than 10. Let's first review the powers of 2 (we're only going to go as far as we need to for an 8-bit byte because IP addresses have 8-bit bytes).
20 = 124 = 16
21 = 225 = 32
22 = 426 = 64
23 = 827 = 128
Given this information, you can convert the binary number 11010011 to decimal as follows.
11010011 =
1*27 + 1*26 + 0*25 + 1*24 + 0*23 + 0*22 + 1*21 + 1*20 =
1*128 + 1*64 + 0*32 + 1*16 + 0*8 + 0*4 + 1*2 + 1*1 =
128 + 64 + 16 + 2 + 1 =
211
IP Addresses and Subnet Masks
You can apply what you know about binary numbers to IP addresses and subnet masks. IP addresses are 32 bits, or four 8-bit bytes, in length. Although computers store IP addresses in binary format, you typically write them in dotted-decimal notation because it's easier to read. Dotted-decimal notation lets you examine an IP address one byte at a time. In standard classful addressing, Class A addresses devote one byte to the network ID and three bytes to the host ID. Class B addresses devote two bytes to the network ID and two bytes to the host ID, and Class C addresses devote three bytes to the network ID and one byte to the host ID.
A subnet mask provides IP, routing protocol, and any other software that deals with addresses a way to determine the network ID and host ID. A subnet mask, like an IP address, is 32 bits in length. Subnet masks are composed of some number of 1 bits followed by enough 0 bits to form a 32-bit value. The bit positions with a 1 correspond with the bit positions in the IP address that are part of the network ID. Therefore, in classful addressing, the subnet mask has eight 1 bits for Class A, sixteen 1 bits for Class B, and twenty-four 1 bits for Class C addresses. These masks are said to be 8, 16, or 24 bits in length, but these numbers refer only to the number of 1 bits because masks really are always 24 bits long.
Suppose you have the Class C address 192.168.18.55 with the regular classful 24-bit Class C subnet mask 255.255.255.0. In binary, the address is (with spaces inserted for readability)
11000000 10101000 00010010 00110111
and the mask is
11111111 11111111 11111111 00000000