Calculating Subnet Range When Using Wildcard Masks

Introduction

In this post I will explain how to calculate the start and end addresses of an IP range when using wildcard masks (aka inverted subnet masks).

Basics of Binary

As I’m sure you know, computers do not think in base-10 decimal notation like humans do, rather, they think in base-2 binary, therefore, the first step is to translate both your IP address and wildcard mask into binary, in this example, I will use the IP 192.168.32.0 and the wildcard mask 0.0.15.255.

In 8 bit binary, we only have 8 possible decimal values in total (128, 64, 32, 16, 8, 4, 2 and 1) with each bit in the octet (fancy name for 8 bits) being used to represent a decimal value. This means the that largest possible value for an octet is 255 and the lowest is, predictably, 0.

Therefore to convert your decimal value to binary you must add 128, 64, 32, 16, 8, 4, 2 & 1 in such as way as to equal your decimal value.

The best way to visualise this is to drawing out a table with similar to the one below. For example, take the value 168, adding 128+32+8 gives me 168 therefore, working left to right, I mark a 1 in columns 128, 32 and 8 to show that these are the bits used to represent this value.

So knowing that both our wildcard mask and IP address are 32 bits (4 octets) long, we can produce tables similar to the ones below.

Decimal Value 128 64 32 16 8 4 2 1
IP (1st Octet) 192 1 1 0 0 0 0 0 0
IP (2nd Octet) 168 1 0 1 0 1 0 0 0
IP (3rd Octet) 32 0 0 1 0 0 0 0 0
IP (4th Octet) 0 0 0 0 0 0 0 0 0
Decimal Value 128 64 32 16 8 4 2 1
Mask (1st Octet) 0 0 0 0 0 0 0 0 0
Mask (2nd Octet) 0 0 0 0 0 0 0 0 0
Mask (3rd Octet) 15 0 0 0 0 1 1 1 1
Mask (4th Octet) 255 1 1 1 1 1 1 1 1

Now let’s place a couple of decimal points in between each octet to make it look a little nicer.

IP: 11000000.10101000.00100000.00000000

Wildcard Mask: 00000000.00000000.00001111.11111111

Now we can get down to business!

Calculations Begin

In a wildcard mask, a binary value of 0 means that the value must remain the same as that of the IP whereas a 1 means that it can have any value.

As you can see, here the whole of the first two octets cannot change therefore we know the first two decimal values of both the start and end address must be equal to 192.168.

We can also see that the last octet can have any value therefore we know that the last octet in the first address must be equal to the lowest binary value possible, which is 0. Following this same logic we can deduce that the value that the first octet will have in the last address in the subnet, must be equal to the highest possible binary value, which is 255.

So after this, we’ve already worked out the bulk of the address, all we need to do is combine our calculations leaving us with the IPs:

Start: 192.168.XXX.0

End: 192.168.XXX.255

However now comes the tricky part, we must calculate the third octet.

IP: 11000000.10101000.00100000.00000000

Wildcard Mask: 00000000.00000000.00001111.11111111

128 64 32 16 8 4 2 1
IP 0 0 1 0 0 0 0 0
Mask 0 0 0 0 1 1 1 1

As you can see the first 4 bits in wildcard mask’s 3rd octet are equal to 0. As bits 1,2 and 4 are both 0 in the mask and in the IP we can safely ignore them. However, the 3rd bit in the wildcard mask is equal to 0 and the value of the third bit in the IP is 1, therefore, we know that the 3rd bit in decimal notation must be equal to 32 and this cannot change.

As for the last 4 bits we know that they can contain any value, due to this we must calculate the upper and lower bounds of this range. The lowest possible decimal value out of all possible values is always going to be 0 since there’s always a chance that each bit will be 0 in binary. If we add up the highest possible value of the last 4 bits (you may wish to refer once again to our handy little table), we discover a maximum possible value in decimal notation is 15.

Now since the 3rd bit is always going to be 32 we simply add 32 to the values we calculated for our lower and upper bounds giving us a lowest possible value of 32 and a maximum possible value of 47.

So after all that work we can now finally put all of this together to give us a result of:

First IP: 192.168.32.0

Last IP: 192.168.47.255

Adjust for Useable Hosts Only (If Needed)

The IP protocol reserves two addresses in each subnet (the first for the network address and the last for the broadcast address) so to calculate for this simply subtract 1 from the decimal value of the last IP and add 1 to the decimal value of the first IP. In this example, this would mean the first useable host 192.168.0.1 and the last useable host is 192.168.47.254