# VirtuallyUseful – Random Tech Babble

## June 10, 2015

### IPv4 Subnetting Tricks

Filed under: Cisco, Networking — Ray Olander @ 1:50 PM

For those of you familiar with numbering systems, or who just want to get to the shortcuts, skip to the section below titled “SHORTCUTS”

Some quick and easy things to learn to help you with subnetting (sorry for the lack of graphics and nice formatting, I need to spend some time learning wordpress):

First, know what binary is.

Binary is a number system based on 2 numbers (0 and 1). Because it only has 2 numbers allowed, it’s called Base2.

In our everyday lives, we use Base10 (where the allowed digits are 0-9).

Second, remember these three rules

ANY number to the power of zero equals 1  (10^0 = 1)

ANY number to the power of one equals itself (10^1 = 10)

A POWER means the number of times a number is multiplied by itself. Ten the the second power (10^2) = 10 x 10 = 100. Two to the third power (2^3) = 2 x 2 x 2 = 8

Third, you need to know that every number in ANY base system gets its value based on its position. It does that because every position has a value.

First, let’s look at position values

Starting from the decimal point and working your way to the left, each position’s VALUE is equal to the DIGIT multiplied by (the BASE to the POWER of the POSITION LEFT OF THE DECIMAL minus 1)

Example1: The number 5 (in Base10).

Following the formula above, we see that “5” is actually equal to 5 x (the BASE to the POWER of the POSITION LEFT OF THE DECIMAL minus 1)

= 5 x (Base10 to the power of “1 position left of the decimal minus 1”)

= 5 x (10 ^ (1 – 1))

= 5 x 10^0

= 5 x 1

=5

Example2: Let’s look at a bigger number: 1234 (one thousand, two hundred and thirty four)

We know that 1234 = 1000 + 200 + 30 + 4

1 is 4 positions left of the decimal, 2 is three positions left of the decimal, 3 is two positions left of the decimal and finally 4 is one position left of the decimal

So what we really have is 1×10^(4-1) + 2×10^(3-1) + 3×10^(2-1) + 4×10^(1-1)

= (1×10^3) + (2×10^2) + (3×10^1) + (4×10^0)

= (1×1000) + (2×100) + (3×10) + (4×1)

= 1000 + 200 + 30 + 4

= 1234

Now let’s apply what we learned about Base10 numbers to Base2 numbers

Ok, so remember, each position’s VALUE is equal to the DIGIT multiplied by (the BASE to the POWER of the POSITION LEFT OF THE DECIMAL minus 1)

Example: 0101 (Base2)

= (0 x 2^(4-1)) + (1 x 2^(3-1)) + (0 x 2^(2-1)) + (1 x 2^(1-1))

= (0 x 2^3) + (1 x 2^2) + (0 x 2^1) + (1 x 2^0)

=(0 x 8) + (1 x 4) + (0 x 2) + (1 x 1)

= 0 + 4 + 0 + 1

=5

A LITTLE more background on number values

Subnetting is done in octets (groups of 8 bits)

The positional values of 8 bits in Base2 = 128, 64, 32, 16, 8, 4, 2, 1

1100 0000 = 128 + 64 = 192

1110 0000 = 128 + 64 + 32 = 224

1111 0000 = 128 + 64 + 32 + 16 = 240 (and going further we get 248, 252, 254 and finally 255 which is the LARGEST value 8 bits can represent in Base2)

You’ll find that the numbers you get when adding like this are the same numbers you’ll see in subnetting

And finally, the last of the rules…

Class “A” = 0.0.0.0 – 127.255.255.255.255

Class “B” = 128.0.0.0 – 191.255.255.255

Class “C” = 192.0.0.0 – 223.255.255.255

We know that 255 is the LARGEST number in any given octet

We know that although we write IPv4 using Base10 numbers (192.168.0.1), the computer only sees them as binary 11000000.101010000.00000000.00000001 (192.168.0.1)

We know that a subnet mask is what tells us which of the IP ADDRESS bits represent the NETWORK and which represent the HOST.

We know that the NETWORK address has ALL HOST BITS SET TO ZERO

We know that the BROADCAST address has ALL HOST BITS SET TO ONE

We know that a VALID HOST ADDRESS must have AT LEAST ONE NON-ZERO BIT

So 255.255.255.252 is the smallest network possible since it consists of a network address, a broadcast address and two IP’s for a device at either end. This is very typical of WAN connections.

We know that a VALID HOST FOR A GIVEN NETWORK is determined by a combination of the NETWORK ADDRESS and the SUBNET MASK

We know that 255 Base10 = 1111 1111 in Base2 (so ALL 8 bits in the octet are set to ONES)

If the subnet mask bit is a ONE, then that portion of the IP ADDRESS represents the NETWORK and if the subnet bit is a ZERO, then that bit of the IP ADDRESS represents the HOST.

Example: 192.168.1.248 with a subnet mask of 255.255.255.240

192 matches the first 255, so it entirely represents the NETWORK portion of the IP ADDRESS

168 matches the second 255, so it entirely represents the NETWORK portion of the IP ADDRESS

1 matches the third 255, so it entirely represents the NETWORK portion of the IP ADDRESS

Now the fun part.

192.168.001.248 (248 in Base10)   = 1111 1000 (Base2)

255.255.255.240 (240 in Base10)   = 1111 0000 (Base2)

So we see that the left four bits of the value 248 represent the NETWORK and that the remaining four bits (1000) represent the host

Therefore, the NETWORK address = 192.168.1 plus all HOST bits zeroed out = 192.168.1.11110000 = 192.168.1.240

The BROADCAST address = 192.168.1 plus all HOST bits set to ONE = 192.168.1.11111111 = 192.168.1.255

The VALID RANGE OF HOSTS = 192.168.1.11110001 (192.168.1.241) through 192.168.1.11111110 (192.168.2.254)

SHORTCUTS (see the examples section below for full explanations) (I’ve changed all formulas to use 256 rather than mixing 256 and 255):

1)In any octet, the max decimal value is 255 (1111 1111 in base 2, or FF in base 16). That means that you can use 256 to quickly calculate class C subnetting since class C networks are all determined in the last octet.

2) The NETWORK BOUNDARY = 256 – NETMASK in that octet. (256-240 = 16 address network)

3) The usable hosts in a class “c” subnet = 256 – 2 – NETMASK (minus 2 for the network and broadcast; 256-2-240 = 14 hosts)

4) The wildcard mask = the inverse of the subnet mask (aka netmask) which also equals 256 – 1 – NETMASK in that octet

a) A netmask of 240 = 1111000 in binary, the inverse of that binary value is 00001111 = 15. So, taking 256 – 1 -240 = 15

5) The VLSM (variable length Subnet Mask) = a slash and the number of bits used in the NETMASK (255.255.255.240 = 8+8+8+4 = /28)

a) This one takes math. Know that 11110000 = 240 (I start from the middle so quicker to add or subtract). Know which bit represents which decimal value (128,64,32,16,8,4,2,1)

b) By knowing 11110000 = 240 = /28 = 16 networks with 14 hosts each, I can quickly determine that a /29 = 11111000 = 240+8 = 248 = 32 networks of 6 hosts each

6) TOTAL POSSIBLE HOSTS PER NETWORK = (2 ^ #host bits,or the bits that are zeros) -2  [the minus 2 is to account for the network and broadcast IP’s]

a) 255.255.255.0 = 11111111.11111111.11111111.00000000, which has 8 zero bits, therefore possible hosts = (2^8)-2 = 256-2 = 254

b) 255.255.255.240 = 11111111.11111111.11111111.11111000, which has 3 zero bits, therefore possible hosts = (2^3)-2 = 8-2 = 6

7) TOTAL POSSIBLE NETWORKS = (2 ^ #network bits for that class,or the bits that are ones)

a) Class C: 255.255.255.0 = 11111111.11111111.11111111.00000000, which has 0 one bits, therefore possible networks = (2^0) = 1

b) Class C: 255.255.255.240 = 11111111.11111111.11111111.11111000, which has 5 one bits, therefore possible networks = (2^5) = 32

SHORTCUT EXAMPLES:

1) The NETWORK BOUNDARY = 256 – NETMASK in that octet

EXAMPLE 1 – CLASS C

Let’s use 192.168.1.0/28

NETWORK BOUNDARY = 256-240=16; that means that a new network starts at every multiple of 16

VALID NETWORKS = 192.168.1.0, 1.16, 1.32, 1.48, 1.64, 1.80, 1.96, 1.112, 1.128, 1.144, 1.160, 1.176, 1.192, 1.208, 1.224, and finally 1.240 (remember we can only go to 255 and 240+16=256)

EXAMPLE 2 – CLASS B

Let’s use 172.16.7.55

NETWORK BOUNDARY = 256 – 240 = 16

VALID NETWORKS = 172.16.0.0, 172.16.16.0, 172.32.0, … , 172.16.240.0

EXAMPLE 3

What network does 192.168.193.74/255.255.255.192 belong to?

256 – 192 = network boundary of 64

valid network are 192.168.193.0, 193.64, 193.128, and 193.192, so the answer is the 192.168.193.64 network (74 is between 64 and 128)

2) The usable hosts in subnet = 256 – 2 – NETMASK (class “C” only for this rule of thumb, see example 5 for the formula for all classes)

EXAMPLE

You have a workstation with the address 192.168.193.74/255.255.255.192. What other IP’s are in the same network as your workstation?

256 – 192 = network boundary of 64 (so we know automatically that there are 62 usable IP addresses total in this network)

valid networks are 192.168.193.0, 193.64, 193.128, and 193.192. That means this address is part of the 192.168.193.64 network (74 is between 64 and 128)

The next network = 192.168.193.128, so we know the BROADCAST address is 192.168.193.127

Answer: that leaves 192.168.193.65 through 192.168.193.73 AND ALSO 192.168.193.75 through 192.168.193.126 (remember you’re .74 and you want the OTHER IP’s)

3) The wildcard mask = the inverse of the subnet mask (aka netmask) which also equals 256 – 1 – NETMASK in that octet

EXAMPLE1

Subnet mask = 255.255.255.192  (option 1: (256 – 1 – that octet) -or- option 2: invert the binary equivalent)

wildcard (option 1) = (256 – 1 – 255).(256 – 1 – 255).(256 – 1 – 255).(256 – 1 – 192) = 0.0.0.63

wildcard (option 2) = we know the 1st THREE numbers are zero (255 always inverts to 0) then tack on 192 in binary and invert it: [0.0.0].11000000 = 0.0.0.00111111 = 0.0.0.63

EXAMPLE2

Subnet mask = 255.255.192.0   (option 1: (256 – 1 – that octet) -or- option 2: invert the binary equivalent)

wildcard (option 1) = (256 – 1 – 255).(256 – 1 – 255).(256 – 1 – 192).(256 – 1 – 0) = 0.0.63.255

wildcard (option 2) = we know the 1st TWO numbers are 0.0 (255 always inverts to 0), we know the last number is 255 (0 always inverts to 255), so for the third octet we do the same calculations: list out the last two octets in binary and invert them: [0.0].11000000. = 0.0.00111111.11111111 = 0.0.63.255

4) The VLSM (variable length Subnet Mask) = a slash and the number of bits used in the NETMASK (255.255.255.240 = 8+8+8+4 = /28)

EXAMPLE

255.255.255.224

To start, we know that 255.255.255 = 8bits + 8bits + 8bits so we’re already at 24 bits. Now we just look at the last octet

Option1: We know that .240 is always 4 bits, we know that .224 is one position left of .240 (number position values), so 3 bits, therefore we know the answer is 24+3 = /27

Option2: We know that the values of the octet are 128, 64, 32, 16, 8 ,4 ,2 ,1 and we know that ADDING the values of each set bit in the octet (1110 0000) gives 128+64+32 = 224

Option3: Memorize decimal values for bit counts. As each bit gets added from the left, the values will always be 128, 192, 224, 240, 248, 252, 254, and 255. Since 224 is always 3rd from the left, we know the answer is 24+3 = /27

5) Find the total possible networks and hosts given a defined class and subnet

Other than to determine which bits by default are used to identify the network, the IP address is irrelevant for this example since the total values are based solely on the subnet mask

EXAMPLE 1

Class network = C

Notes

For a standard class C address, we know that the first THREE octets are the “normal” subnet mask bits.

We know that 255 = 8 bits so we have a total of 8+8+8 = 24 bits defining the network

There are 32 bits in an IPv4 address, so there are 32 – 24 = 8 bits defining the hosts

Calculate possible networks

Using the formula ( 2 ^ # network bits) = total networks possible (assuming your gear also handles “zero networks”, otherwise [(2^n)-1] )

2 ^ 24 = 16,777,216 possible networks

Calculate possible hosts

Using the formula ( 2 ^ # host bits ) – 2 = (2 ^ 8) – 2 = 256 – 2 = 254

We subtract 2 to account for the network and the broadcast values

To verify, use the class “c” shortcut of 254 – (last octet of the netmask) = 254 – 0 = 254 hosts per network

EXAMPLE 2

Class network = B

Notes

For a standard class B address, we know that the first TWO octets are the “normal” subnet mask bits.

We know that 255 = 8 bits so we have a total of 8+8 = 16 bits defining the network

There are 32 bits in an IPv4 address, so there are 32 – 16 = 16 bits defining the hosts

Calculate possible networks

Using the formula ( 2 ^ # network bits) = total networks possible (assuming your gear also handles “zero networks”, otherwise [(2^n)-1] )

2 ^ 16 = 65,536 possible networks

Calculate possible hosts

Using the formula ( 2 ^ # host bits ) – 2 = (2 ^ 16) – 2 = 65,536 – 2 = 65,534 hosts per network

We subtract 2 to account for the network and the broadcast values

EXAMPLE 3

Class network = A

Notes

For a standard class A address, we know that the FIRST octet is the “normal” subnet mask bits.

We know that 255 = 8 bits so we have a total of 8 bits defining the network

There are 32 bits in an IPv4 address, so there are 32 – 8 = 24 bits defining the hosts

Calculate possible networks

Using the formula ( 2 ^ # network bits) = total networks possible (assuming your gear also handles “zero networks”, otherwise [(2^n)-1] )

2 ^ 8 = 256 possible networks

Calculate possible hosts

Using the formula ( 2 ^ # host bits ) – 2 = (2 ^ 24) – 2 = 16,777,216 – 2 = 16,777,214 hosts per network

We subtract 2 to account for the network and the broadcast values

I hope you found this useful. Perhaps at a later date I will have time to add graphics of this process. There are many sites on the Internet that deal with this topic. A quick search brought up this site that was quite informational (and formatted much nicer than mine!): http://www.subnetting.net/Tutorial.aspx