Example:

IP: 128.42.5.4

In binary: 10000000 00101010 00000101 00000100

Subnet: 255.255.248.0

How could you determine the prefix, network, subnet, and host numbers?

up vote 130 down vote accepted

Calculating the Netmask Length (also called a prefix):

Convert the dotted-decimal representation of the netmask to binary. Then, count the number of contiguous 1 bits, starting at the most significant bit in the first octet (i.e. the left-hand-side of the binary number).

255.255.248.0   in binary: 11111111 11111111 11111000 00000000
                           -----------------------------------
                           I counted twenty-one 1s             -------> /21

The prefix of 128.42.5.4 with a 255.255.248.0 netmask is /21.

Calculating the Network Address:

The network address is the logical AND of the respective bits in the binary representation of the IP address and network mask. Align the bits in both addresses, and perform a logical AND on each pair of the respective bits. Then convert the individual octets of the result back to decimal.

Logical AND truth table:

Logical AND

128.42.5.4      in binary: 10000000 00101010 00000101 00000100
255.255.248.0   in binary: 11111111 11111111 11111000 00000000
                           ----------------------------------- [Logical AND]
                           10000000 00101010 00000000 00000000 ------> 128.42.0.0

As you can see, the network address of 128.42.5.4/21 is 128.42.0.0

Calculating the Broadcast Address:

The broadcast address converts all host bits to 1s...

Remember that our IP address in decimal is:

128.42.5.4      in binary: 10000000 00101010 00000101 00000100

The network mask is:

255.255.248.0   in binary: 11111111 11111111 11111000 00000000

This means our host bits are the last 11 bits of the IP address, because we find the host mask by inverting the network mask:

Host bit mask            : 00000000 00000000 00000hhh hhhhhhhh

To calculate the broadcast address, we force all host bits to be 1s:

128.42.5.4      in binary: 10000000 00101010 00000101 00000100
Host bit mask            : 00000000 00000000 00000hhh hhhhhhhh
                           ----------------------------------- [Force host bits]
                           10000000 00101010 00000111 11111111 ----> 128.42.7.255

Calculating subnets:

You haven't given enough information to calculate subnets for this network; as a general rule you build subnets by reallocating some of the host bits as network bits for each subnet. Many times there isn't one right way to subnet a block... depending on your constraints, there could be several valid ways to subnet a block of addresses.

Let's assume we will break 128.42.0.0/21 into 4 subnets that must hold at least 100 hosts each...

subnetting

In this example, we know that you need at least a /25 prefix to contain 100 hosts; I chose a /24 because it falls on an octet boundary. Notice that the network address for each subnet borrows host bits from the parent network block.

Finding the required subnet masklength or netmask:

How did I know that I need at least a /25 masklength for 100 hosts? Calculate the prefix by backing into the number of host bits required to contain 100 hosts. One needs 7 host bits to contain 100 hosts. Officially this is calculated with:

Host bits = Log2(Number-of-hosts) = Log2(100) = 6.643

Since IPv4 addresses are 32 bits wide, and we are using the host bits (i.e. least significant bits), simply subtract 7 from 32 to calculate the minimum subnet prefix for each subnet... 32 - 7 = 25.

The lazy way to break 128.42.0.0/21 into four equal subnets:

Since we only want four subnets from the whole 128.42.0.0/21 block, we could use /23 subnets. I chose /23 because we need 4 subnets... i.e. an extra two bits added to the netmask.

This is an equally-valid answer to the constraint, using /23 subnets of 128.42.0.0/21...

subnetting, 2nd option

Calculating the host number:

This is what we've already done above... just reuse the host mask from the work we did when we calculated the broadcast address of 128.42.5.4/21... This time I'll use 1s instead of h, because we need to perform a logical AND on the network address again.

128.42.5.4      in binary: 10000000 00101010 00000101 00000100
Host bit mask            : 00000000 00000000 00000111 11111111
                           ----------------------------------- [Logical AND]
                           00000000 00000000 00000101 00000100 -----> 0.0.5.4

Calculating the maximum possible number of hosts in a subnet:

To find the maximum number of hosts, look at the number of binary bits in the host number above. The easiest way to do this is to subtract the netmask length from 32 (number of bits in an IPv4 address). This gives you the number of host bits in the address. At that point...

Maximum Number of hosts = 2**(32 - netmask_length) - 2

The reason we subtract 2 above is because the all-ones and all-zeros host numbers are reserved. The all-zeros host number is the network number; the all-ones host number is the broadcast address.

Using the example subnet of 128.42.0.0/21 above, the number of hosts is...

Maximum Number of hosts = 2**(32 - 21) - 2 = 2048 - 2 = 2046

Finding the minimum netmask which contains two IP addresses:

Suppose someone gives us two IP addresses and expects us to find the longest netmask which contains both of them; for example, what if we had:

  • 128.42.5.17
  • 128.42.5.67

The easiest thing to do is to convert both to binary and look for the longest string of network-bits from the left-hand side of the address.

128.42.5.17     in binary: 10000000 00101010 00000101 00010001
128.42.5.67     in binary: 10000000 00101010 00000101 01000011
                           ^                           ^     ^
                           |                           |     |
                           +--------- Network ---------+Host-+
                             (All bits are the same)    Bits

In this case the minimum netmask would be /25

NOTE: If you try starting from the right-hand side, don't get tricked just because you find one matching column of bits; there could be unmatched bits beyond those matching bits. Honestly, the safest thing to do is to start from the left-hand side.

The answer above hits the nail on the head perfectly. However, when I first started out, it took me a few different examples from a couple of sources for it to really hit home. Therefore, if you're interested in other examples, I wrote a few blog posts on the subject - http://www.oznetnerd.com/category/subnetting/

Admins, if this post is considered spam, please feel free to delete it.

Edit: As per YLearn's suggestion, I'll try to grab the relevant parts from Part 1 of my series, without pasting the whole entry here.

Let's use 195.70.16.159/30 as an example.

As it is a /30, we know the host portion is going to be in the fourth octet. Let's convert that to binary:

128 64 32 16  8  4 2 1
SN  SN SN SN SN SN H H
 1   0  0  1  1  1 1 1

Now to find out the network address all we do is add the SN bits that have a 1 underneath them, together. (128 + 16 + 8 + 4 = 156).

When you add this 156 to the first three octets of the address, we’re left with the Network Address 195.70.16.156.

Now, as we know that the first usable address is always the Network Address plus one, all we need to do is perform the following calculation: (156 + 1 = 157).

This gives us a First Usable Address of 195.70.16.157.

Now let’s skip the Last Usable Address for a moment and find the Broadcast Address. To find out what it is, all we need to do is add all of the H bits together (regardless of whether they are a 1 or a 0) and then add this number to the Network Address. (2 + 1 + 156 = 159).

This gives us a Broadcast Address of 195.70.16.159.

And finally, let’s work out the last usable address. This process is similar to finding the First Usable Address, however, instead of adding one to the network address, we actually subtract one from the Broadcast Address. (159 – 1 = 158).

This gives us a Last Usable Address of 195.70.16.158.

And there we have it! Our temaplte is complete. For easy reference, here it is again:

  • Network Address: 195.70.16.156
  • First Usable Address: 195.70.16.157
  • Last Usable Address: 195.70.16.158
  • Broadcast Address: 195.70.16.159

As a shortcut, you can also use this formula. It works on subnets of any size:

  • First Usable Address = Network Address + 1
  • Broadcast Address = Next Network Address – 1
  • Last Usable Address = Broadcast Address – 1
  • 4
    Not spam, but could you please edit your post to provide more details from the link? To avoid problems with link rot, the community prefers that you quote important content and provide the link as reference whenever possible. – YLearn Jun 4 '15 at 1:02
  • Thanks for the comment YLearn. The information is broken across three blog posts, all of which should really be read together. If I were to copy and paste bits and pieces I feel like they will lose their value and may even cause confusion. Edit: I'll give it a try though :) – OzNetNerd Jun 4 '15 at 4:21
  • 2
    Tiny (almost insignificant) caveat: the Last Usable Address formula at the bottom works for all subnets except a /31... see RFC 3021. It's a small but relevant exception if someone tried to use your algorithm in code. – Mike Pennington Jun 4 '15 at 7:34

Example:

IP: 128.42.5.4

In binary: 10000000 00101010 00000101 00000100

Subnet: 255.255.248.0

How could you determine the prefix, network, subnet, and host numbers?

      32768     16384  8192  4096  2048  1024   512   256  ----> Binary
        128       192   224   240   248   252   254   255  ----> Sunet Mask
        /17       /18   /19   /20   /21   /22   /23   /24  ----> CIDR  
      32766     16382  8190  3094  2046  1022   510   254  ----> Host


      128     64    32     16     8     4    2     1   ----> Binary
      128    192   224    240   248   252   254   255  ----> Sunet Mask
      /25    /26   /27    /28   /29   /30   /31   /32  ----> CIDR  
      126     62    30     14     6     2    *     -   ----> Host 

     128        64        32       16        8         4         2        1
  10000000   01000000  00100000 00010000  00001000  00000100  00000010   00000001

   Example 
   Network=192.168.1.0 /24;  
   Network Address with Subnet mask =  192.168.1.0 subnet 255.255.255.0 
   Ip address range 192.168.1.0----192.168.1.255
   Fist available ip address  192.168.1.1; 
   Last available ip address  192.168.1.254; 
   Broadcast address = 192.168.1.255;
   254 Host

   Network=192.168.1.0 /25;
   Network Address with Subnet mask =  192.168.1.0 subnet 255.255.255.128
   Ip address range 192.168.1.0----192.168.1.128
   Fist available ip address  192.168.1.1; 
   Last available ip address  192.168.1.126;
   Broadcast address = 192.168.1.127;  
   126 Hosts

   When the CIDR increased ex. /24. /25.  the network will divided by the 
   binary number.
   /25  increase network   0-128| 128- 256 |                   you will have 2 Networks 
   /26  increase network   0-64 | 64 - 128 | 128-192 | 192-256 you will have 4 Networks 
    .
    .
    .
   /32......

protected by YLearn Jun 4 '15 at 1:02

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.