How to Subnet -- The Words I was recently asked to provide a written version of the explanation I gave on how to analyze an IP address and subnet mask to determine the subnet number, host number, and the broadcast address. It seemed that if it was helpful once, it just might be helpful again! So here it is. Bob Cromwell, July 1996 ------------------------------------------------------------------------------- We're given: IP address = 192.55.12.120 Subnet mask = 255.255.255.240 We're asked: What's the subnet number? What's the host number? How do we broadcast on that subnet? First things first -- according to the classical rules (see slides 3-14 through 3-19), we can say: [1] The first octet of the IP address is 192. [2] Therefore, it is a class C IP address. Why? [a] Method number one -- you memorized the table of allowable ranges: Class A 1-126 Class B 128-191 Class C 192-223 Class D 224-239 Class E 240-248 [b] Method number two -- you know how it works, and figure it out from there. Convert it to binary and see where the first zero appears. Keep in mind that 0.0.0.0 is not allowed, and 127.0.0.0 is reserved for loopback. So, converted to binary, 192 is 11000000. The first zero is the third bit, and therefore it's class C. [3] To the outside world, unaware of any subnet tricks we're doing inside out autonomous network, this is on the 192.55.12.0 network. In other words, the netid is the leading "192.55.12". [4] The hostid is the last octet only, so both the subnet number and the host number come from just the last eight bits. Remember: Netid The bits of the IP address that specify the network ID, using the classical Class A/B/C rules. Hostid The bits of the IP address that specify the host ID, using the classical Class A/B/C rules. Subnet number Those bits within the hostid that specify the subnet. Host number Those bits within the hostid that specify the host. Or, the hostid bits left over after the subnet bits are accounted for. Now, the first three octets of the subnet mask -- the 255.255.255 part -- we expected at least that since it's a class C network address. But, the last octet is not zero, so subnetting is going on. Remember, any time that the subnet mask is not exactly what you'd expect going by the classical Class A/B/C rules only, the network is subnetted. Let's ignore (for just a while!) the octets that make up the netid: the first three octets. We want to look at the hostid, or the last octet, in detail, as well as the corresponding portion of the subnet mask. Hostid: Decimal 120 Binary 01111000 Corresponding part Decimal 240 of subnet mask: Binary 11110000 Now we take one simple step: FORMAL: Logically AND those two bit patterns NOT SO FORMAL: The subnet mask has 1's in the first four bits. So keep the first four bits of the hostid, and add four 0's where the subnet mask has 0's. Either was you get: 01110000, which converts to 112 in decimal. ANSWER NUMBER ONE: The subnet number is 112. Now for the next step: FORMAL: Logically AND the hostid with the logical inverse of the corresponding bits of the subnet mask. NOT SO FORMAL: It's kind of like what we just did, except the other way around.... Everywhere the corresponding subnet mask bit is 1 we write "0", and everywhere it's 0 we write whatever's in the corresponding hostid bit. Either way you get: 00001000, which converts to 8 in decimal. ANSWER NUMBER TWO: The host number is 8. Notice that the leading "0111" in the subnet number and the final "1000" in the host number came from the two sections of the 8-bit hostid. One from before the division made by the end of the subnet bits, and one after. Also notice that the sum of the subnet number and the host number gives us the hostid, telling us we have no missing or extra bits. All that's left is to build the broadcast address: [1] Remember those 3 octets of netid? Start with "192.55.12". [2] Start the hostid (the last octet in this example) with the bits making up the subnet number: "0111". [3] Finish the hostid with all 1's in the host number: "1111". [4] The hostid is "0111" followed by "1111", or 01111111. [5] Converted to decimal, 01111111 is 127. That's the last octet. ANSWER NUMBER THREE: The broadcast address is 192.55.12.127. While we're playing with bits.... Neither the subnet number nor the host number can be all 0's or all 1's, so there are two disallowed combinations when calculating the total possible subnet and host numbers. In turn that means that we can't have only one subnet bit or only one host bit. For Class C addresses, we can calculate: Subnet Number of possible Number of possible Total possible bits subnets hosts on each subnet hosts ------ -------------------- --------------------- -------------- 0 0 2^8 - 2 = 256-2 = 254 254 2 2^2 - 2 = 4-2 = 2 2^6 - 2 = 64-2 = 62 2*62 = 124 3 2^3 - 2 = 8-2 = 6 2^5 - 2 = 32-2 = 30 6*30 = 180 4 2^4 - 2 = 16-2 = 14 2^4 - 2 = 16-2 = 14 14*14 = 196 5 2^5 - 2 = 32-2 = 30 2^3 - 2 = 8-2 = 6 30*6 = 180 6 2^6 - 2 = 64-2 = 62 2^2 - 2 = 4-2 = 2 62*2 = 124 Notice that the first case in that table is for not subnetting at all. We do lose some possible addresses when subnetting, and only 3, 4, and 5 subnet bits provide really reasonable choices for subnetting Class C networks. OK, so you still aren't tired of playing with bits? Once more through the original problem, except REALLY formal this time! Let: I = IP address S = subnet mask D = default subnet mask, as per the rules: Class A: 255.0.0.0 Class B: 255.255.0.0 Class C: 255.255.255.0 X AND Y = logical AND of X and Y X OR Y = logical AND of X and Y NOT(X) = logical inverse of X Now we can say: netid = I AND D hostid = I AND NOT(D) subnet number = I AND NOT(D) AND S [which is hostid AND S] host number = I AND NOT(D) AND NOT(S) [which is hostid AND NOT(S)] broadcast address = (IP AND D) OR (IP AND NOT(D) AND S) OR NOT(S) = netid OR subnet number OR NOT(S) = (IP AND S) OR NOT(S) Simple, huh? That's actually how the TCP/IP software calculates these.