WORKING OUT IPV4 LAN ADDRESS RANGES

Introduction

There are occasions when you may need to work out the first and last IP addresses in a network given only an IP address in that network and its subnet mask. This is useful when calculating the number of addresses in the network or when you need to process each IP address separately.

AN EXAMPLE

The following example shows how the calculation works. First, choose an IP address in the range and the subnet mask.

Current network address 81.138.219.45   = 01010001.10001010.11011011.00101101
Subnet mask             255.255.255.248 = 11111111.11111111.11111111.11111000

To find the first address, which is also the network address, you bit-wise AND the given IP address and the subnet mask:


                        81.138.219.45   = 01010001.10001010.11011011.00101101
                        255.255.255.248 = 11111111.11111111.11111111.11111000
                    AND -----------------------------------------------------
                                          01010001.10001010.11011011.00101000
                  gives 81.138.219.40

To find the last address, which is also the broadcast address, you OR the given IP address with the one's complement of the subnet mask. The one's complement of the mask is obtained by flipping each bit.

Subnet mask             255.255.255.248 = 11111111.11111111.11111111.11111000
All bits flipped        0.0.0.7         = 00000000.00000000.00000000.00000111
                              
                        81.138.219.45   = 01010001.10001010.11011011.00101101
Complement of mask      0.0.0.7         = 00000000.00000000.00000000.00000111 
                     OR -----------------------------------------------------
                                          01010001.10001010.11011011.00101111
                  gives 81.138.219.47

Using this calculation you can determine:

FIXED IP ADDRESSES

Using the calculation above shows why, when you order a broadband service with fixed addresses, you can only have 1, 5 or 13 available addresses, etc. The 13 arises with a 28-bit subnet mask giving 16 addresses of which the network, router and broadcast each use one, leaving 13 for customer use.

COMPLEMENTS

There are two types of bit complements:

  1. a one's complement that just switches or flips each bit. This is the operation that the C ~ operator performs.
  2. a two's complement that is a method of representing signed numbers in a computer. You convert between a positive integer and its negative counterpart by flipping all bits and adding one. The same process is used to reverse the calculation.