Some of the links posted above aren't valid and some involve binary math/conversions. I'm adding some formulas here so they are available directly in the forum without depending on external sites..
When I was teaching network fundamentals back in the late 90's, subnetting was always the most difficult topic for students to understand. I put a spreadsheet together to illustrate the binary representation as well as dotted decimal and CIDR netmask values. In doing so, I recognized some formulas that allowed me to do calculations quickly in my head. I found that most students quickly grasped the logic of these formulas with minimal memorization. The spreadsheet with most of the following discussion can still be downloaded from my website -
http://www.innotechcg.com/downloads/netmask.zip. Note that where possible, the most simple math is used, even counting on fingers! No binary conversions or other difficult mental gymnastics are needed.
In Your Head Subnetting TricksBasic Knowledge- Remember that "256" is a "magic" number in networking and subnetting.
- Memorize the binary values - 1,2,4,8,16,32,64,128 - you'll see them a lot!
- Valid subnet masks are limited to the values that result by subtracting those binary values from 256:
255, 254, 252, 248, 240, 224, 192, and 128. No other value is permitted as a subnet mask value. These should be committed to memory, but you can easily calculate them when needed. - The subnet mask-value (other than 255) can appear in any ONE of the 4 octets. Any octet value to the left must be 255, any to the right must be 0. For example - 255.255.248.0 is valid, 248.255.255.0 is not. Each octet to the right containing a zero represents a multiple of 256.
Now that you have the binary values 1,2,4,8,16,32,64,128 and subnet values 192, 224, 240, 248, 252, 254, & 255, recognize that in the following math problems, these are the ONLY possible answers for the magic formulas! This makes it easy to check your work.
Based on this understanding, the subnet 255.255.248.0 can be understood using some basic arithmetic:
- the "mask value" is in the third octet (248);
- a mask value of 248 represents 8 groups of addresses (256-248=8) since a zero is to the right,
we have 8 groups of 256 units (8*256=2048) or 2048 addresses per subnet - 2046 usable.
If the mask value was in the last octet, it would directly represent the number of hosts. - The number of subnets for a .248 mask value is 256/groups or for this example: 256/8=32
Magic FormulasUsing the magic number and some simple formulas, you can figure out any subnet requirement - mask, # of hosts, or number of subnets:
Subtract the netmask value from 256 to find the number of hosts or host groups per subnet. If there are octets with ZERO values following the mask octet, multiply the result by 256 for each following zero.
1. mask=255.255.255.192, 256-192=64, less 2 yields 62 hosts; 256/64=4 subnets
2. mask=255.255.240.0, 256-240=16, following zero, so multiply by 256=4096, less 2 yields 4094 hosts; 256/16=16 subnets
3. mask=255.252.0.0, 256-252=4, times 256=1024, times 256=262144, less 2 yields 262142[/list]
In examples 2 and 3 - any zero values to the right of the subnet value represent groups of 256 addresses. You must find the number of address groups, then multiply that by 256 for each zero to the right.
Divide 256 by the number of subnets needed to determine the # of hosts per subnet.You
MUST round the number of subnets UP to the nearest power of 2 (1, 2, 4, 8, 16, etc.)
Need 8 subnets - divide 256 by 8 to get 32 hosts* per subnet.
Need 20 subnets, round up to 32, divide 256 by 32 to get 8 hosts* per subnet.
*if mask value is not in the 4th octet, the result is host-groups - multiply by 256 for each following octet to get total hosts.
Divide 256 by the number of hosts or host groups to determine the number of subnets.Mask is 255.255.224.0, subtract 224 from 256 (32), divide 256 by 32 (8)
Subtract the number of hosts from 256 to determine the netmask.Need 4 subnets - divide 256 by 4 - yields 64 hosts per subnet. 256-64=192, netmask is 255.255.255.192
CIDR / Dotted-Decimal ConversionsConverting dotted-decimal netmast to CIDR - 255.255.240.0
For each octet containing 255, add 8 to the CIDR value (8 + 8 = 16)
Count the valid subnet values from 128 to the mask value (128,192,224,240 is 4 steps)
Add the two values - the result is 8+8+4 or 20, the CIDR netmask is /20.
Converting CIDR to dotted-decimal To determine the dotted decimal netmask of a /22 maskbit notation, keep subtracting 8 from the number of bits - every time you can subtract 8, that octet represents 255. Any remainder indicates how many bits are on in the next octet - calculate the value of the netmask. Any remaining octets are zero.
Subtract 8 from 22 - first octet is 255, remainder is 14
Subtract 8 from 14 - second octet is 255, remainder is 6
6 bits remain, binary values are 128+64+32+16+8+4, third octet is 252
0 bits remain, so fourth octet is 0, resulting netmask in dotted.decimal notation is 255.255.252.0
Examples:Network address is 10.24.0.0, need 50 subnets1. If first/last subnets are NOT allowed (unusual), add 2
2. Increase the required value to the nearest power of 2 (50 => 64)
3. Divide 256 by the number of subnets determined above (256/64 = 4)
This is host-groups - multiply by 256 to get subnet size (1024)
4. Subtract hosts/host-groups from 256 (256-4=252) to get mask value
Netmask is 255.255.252.0, 64 subnets, 1024 addresses, 1022 hosts/subnetNetwork is 172.16.12.0, mask is 255.255.248.0 - how many subnets? How many hosts/subnet?1. Identify mask-value (248)
2. Subtract from 256 (256-248=8)
3. Zero follows, so multiply by 256 for each following zero (8*256=2048) to find addresses/subnet
4. Subtract 2 from addresses/subnet to get hosts/subnet (2048-2=2046)
5. Divide 256 by Hosts/host-group value to get # of Subnets (256/8=32); Subtract 2 if first/last subnets are NOT permitted.
# of subnets: 32, Hosts/subnet: 2046