First you'll want to remember to use binary conversion chart; anyone familiar with computers should feel at home with these; they're the same values used for unit sizes on file systems.
128 64 32 16 8 4 2 1Each one of these represent a bit in the octet of the subnet mask; added together, the "turned on" bits will give you the value of that octet (binary can only hold a value of 1 or 0, true or false, on or off).
10110101 will be 128+32+16+4+1 so the octet would be 181
Nitty Gritty
There are two ways to subnet, by desired host, and by desired networks. Doing by networks will usually result in an excess of host addresses. However doing it by hosts, if done incorrectly, can result in too few addresses and limit the scalability of your networks.Address space we'll be working with:
Network address: 172.16.0.0
Subnet Mask: 255.255.0.0
CIDR: /16
By Network
Number of desired networks will be twenty (20).Begin by using the chart above to see how many network bits you would need to get at least 20.
You'll want to add the number of bits onto the left most octet available.
128 64 32 16 8 4 2 1
0 0 0 1 0 1 0 0
^---------^
5 bits
255.255.0.0 = 11111111.11111111.00000000.00000000
255.255.248.0 = 11111111.11111111.11111000.00000000
^------^
5 bits added
The subnet mask decimal value is calculated by adding up the binary values according to our little chart. Where as your CIDR notation is the number of actual bits added up (one full octet is equal to eight (8)).
128 64 32 16 8 4 2 1The way I usually calculate usable range is to subtract the subnet mask from 256. However technically the formula is 2^n where n is equal to the number of available host bits in the left most octet.
1 1 1 1 1 0 0 0
128+64+32+16+8 = 248
Subnet Mask: 255.255.248.0
11111111.11111111.11111000.00000000
CIDR: /21
256-248 = 8So our network range becomes 172.16.0.0-172.16.0.7 (remember that the first IP is the network address, and the last is the broadcast; so host IPs are 172.16.0.1 through 172.16.0.6).
2^3 = 2x2x2 = 8
Finding the total number of available networks is similar.
2^n where n = number of network bits added
so 2^5 = 32 networks available
By Host
Calculating by host requirement is almost the same thing, except backwards...ishNumber of desired hosts will be twenty (20)
Start off with the same thing as before, find the number of bits requires for the number you're looking for, this time hosts instead of networks.
128 64 32 16 8 4 2 1Now for the backwards part. The five bits will be how many host bits will remain (so you would need the inverted amount of network bits)
0 0 0 1 0 1 0 0
^---------^
5 bits
255.255.0.0 = 11111111.11111111.00000000.00000000Everything aside from that one step is the same.
255.255.255.224 = 11111111.11111111.11111111.11100000
^-------^
5 bits remaining
Usable range is 172.16.0.0-172.16.0.32 with a CIDR of /27 (you can subtract host bits from 32 if that's any easier for you) and usable number of networks being 2^11 which is 2048.
And that's about it. I find this logic a lot easier to understand than the things I've seen elsewhere.