We were talking about this in IRC the other day so I thought I'd write up a explanation for anyone who would care to know a little more.
I'm going to assume here that you already know how binary works, and can perform some simple
converion either in your head or on paper. I'm also assuming that you know the difference
between a class A, B, and C IP address.
The purpose of a subnetting is to make more logical divisions (subnetworks) out of a given
IP address range. In a class A network, there are 126 useable networks, and 16777214 hosts
per network. Kinda ridiculous and defineatly not very useful. To address this problem the
practice of subnetting was put into use. Every host on every network must have an
appropriate subnet mask or it can not comunicated on the network. To acheive the additional
networks, bits are 'borrowed' from the host portion of the address.
Looking at IP addresses in the dotted decimal format, the network vs. hosts bits are broken
down like this:
class A -- N.H.H.H
Class B -- N.N.H.H
class C -- N.N.N.H
The network number for a network always has the hosts bits turned off (o). As well, the
address for every host on the network is when all host bits are turned on (1).
So, let's take a look at what exactly an IP address is. An IP address is a 32 bit binary
string that is used to identify a host on a network. For readibility's sake, this string is
commonly represented as a dotted decimal quad that looks something like:
192.168.69.132
But keep in mind, this is what it really looks like:
11000000101010000100010110000100
Well, this doesnt look fun at all now does it. That is what IP looks like to a router or a
host, so we must too.
Now let's look at a default subnet mask for our above example class C IP address:
255.255.255.0
Which in binary is:
11111111111111111111111100000000
To get the network address from these two numbers, the numbers are AND'ed together
11000000101010000100010110000100
A N D
11111111111111111111111100000000
E Q U A L
11000000101010000100010100000000
Convert this into our dotted decimal notation and we have:
192.168.69.0
This number is used by routing protocols to identify this network among other network on a
LAN or WAN. But what does this all mean? This means that our example IP belongs to the
132nd host on the network 192.168.69.0. it also means that there are a possible 254 hosts
on that same network.
But, what if the subnetmask was not 255.255.255.0? Let's say the subnet mask is
255.255.255.192 instead. Performing the AND'ing again we get:
11000000101010000100010110000100
A N D
11111111111111111111111110000000
E Q U A L
11000000101010000100010110000000
Whoa! In our dotted decimal that makes 192.168.69.128 as the network number! So now our
address is the 4th host on network 192.168.69.128, out of a possible 126. And because this
is now a subnetted address, the network is actually the second of two subnetworks.
As mentioed above, a class C address uses the final 8 bits to represent the host portion of
an address. With our example subnet of 255.255.255.192 we borrowed 2 bits, giving us two
usable subnets. The formula to figure out the useable hosts or subnets it to raise 2 to the
number of bits available and minus 2 ((2^n)-2)). With our example we have:
(2^2)-2
4-2
2
Giving us 2 useable subnets (the first is the network number, the last is the broadcast,
hence the minus 2. As well, when subnetting you may not borrow 1 bit, or leave 1 bit for
the host portion. At least two bits must occupy either network or host portion at all
times).
For the hosts we have 6 bits:
(2^6)-2
64-2
62
62 useable host addresses per subnet. Now wait a moment... With our default subnet we had
one network with two hundred and fifty-four hosts, now we have two networks with sixty-two
hosts per subnetwork? Where did the other 132 hosts addresses go?!?! This is the downside
to subnetting. While you lose available host addresses, the benefits of subnetting far
outweigh this and is well beyond how much detail I want to go into =�.
Let's looks at a little more complicated example. We want to find the network number, host
number for this IP, total number of subnets, and total number hosts:
IP: 24.67.85.144
Mask: 255.255.192.0
first we do a binary AND:
00011000010000110101010110010000
A N D
11111111111111111100000000000000
E Q U A L
00011000010000110100000000000000
Into dotted decinal we have a network number of 24.67.64.0. This means that our host is number
5520 on this network! To finish the question, it's a class A network so we borrowed 10 bits:
(2^10)-2
1024-2
1022 Subnets
And we left 14 bits in the host portion:
(2^14)-2
16384-2
16382 Hosts
Now that we can find a subnet, let's make some. Let's say we want to subnet the class B
address of 172.15.0.0 into a minimum of 325 subnetworks. Using the above formula:
(2^8)-2 = 254 too low
(2^9)-2 = 512 OK
So if we are to borrow 9 bits then our subnet mask is 255.255.255.128. But let's look at
how we arrive at our network numbers by using binary again. We start with the address of
172.15.0.0, which gives the following meaning to the bits:
NNNNNNNNNNNNNNNNSSSSSSSSSHHHHHHH
Because this is an assigned address, we cannot change the first two octets. knowing those
are constant we have a subnet range of 172.15.0 - 172.15.255 . in binary this is:
172.15.00000000 - 172.15.11111111
To get get our subnetwork numbers we count up the subnetwork bits in binary one at a time:
Binary -- Decimal
172.15.000000000|0000000 -- 172.15.0.0 (unuseable)
172.15.000000001|0000000 -- 172.15.0.128
172.15.000000010|0000000 -- 172.15.1.0
172.15.000000011|0000000 -- 172.15.1.128
172.15.000000100|0000000 -- 172.15.2.0
172.15.000000101|0000000 -- 172.15.2.128
to
172.15.111111011|0000000 -- 172.15.253.128
172.15.111111100|0000000 -- 172.15.254.0
172.15.111111101|0000000 -- 172.15.254.128
172.15.111111110|0000000 -- 172.15.255.0
172.15.111111111|0000000 -- 172.15.255.128 (unuseable)
And likewise, in any given subnetwork we count up each host bit to get the host numbers:
Binary -- Decimal
172.15.010101011|0000000 172.15.85.128 (Network)
172.15.010101011|0000001 172.15.85.129
172.15.010101011|0000010 172.15.85.130
172.15.010101011|0000011 172.15.85.131
to
172.15.010101011|111100 172.15.85.252
172.15.010101011|111101 172.15.85.253
172.15.010101011|111110 172.15.85.254
172.15.010101011|111111 172.15.85.255 (Broadcast)
And that is how we subnet!