How do you calculate the prefix, network, subnet, and host numbers?

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?

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.

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:

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

Remember that our IP address in decimal is:

128.42.5.4      in binary: 10000000 00101010 00000101 00000100

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...

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.

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...

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

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

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.

• Is there a specific reason you choose subnet 0,2,4,6 with /23 bit subnets in the second colored graph? Opposed to 0,1,2,3 with /24 bit subnet Oct 27, 2021 at 11:40
• The reason OP came up with 0,2,4,6 is because /21 gets you something like 21 1s and 11(3 + 8) 0s. And since we need 4 subnets, we take the next 2 hosts bits(22nd and 23rd) and thus being able to use the 22nd and 23rd to create 4 new subnets. And for creating 4 subnets, we use the 22nd and 23rd bit as 00, 01, 10, 11 leaving the last 9 bits as is. So the first subnet would be 21 1s and rest all 0s. @nd subnet would be 21 1s, followed by a 0 (22nd bit) and then 1(23rd bit) and then rest all 0s. 10000000 00101010 00000001 00000000 which is 128.42.2.0. Similarly 00000010 is 4. 00000011 is 6 Jul 30 at 7:50

Part 1 of 2

IPv4 Math

It cannot be stressed enough that you must do IPv4 math in binary. Every network engineer has tried to figure out a way to do it all in decimal, as you will*. The problem is that 10 (decimal) is not a power of 2 (binary), so decimal and binary do not easily convert between each other the way that hexadecimal (base 16) easily converts to and from binary because 16 is a power of 2. Using dotted-decimal notation for IPv4 was an early mistake that cannot now be corrected, but IPv6 adopted the use of hexadecimal from the very beginning, and it is easy to convert between hexadecimal and binary.

If you do not have an IP calculator (disallowed in exams for network courses or certifications), make a chart of the values of the bits in an octet. In binary, each bit value is 2 times the same digit value in the next less-significant digit. Each digit is the number base times the same digit value in the next less-significant digit. This is also true for all number bases, including decimal, where each digit value is 10 times the value of the same digit value in the next less-significant digit. Where decimal is powers of 10, binary is powers of 2. Notice that for each bit number in the table, the corresponding value is 2 to the power of the bit number.

+-------------------------------------------------------+
¦ BIT # ¦  7  ¦  6  ¦  5  ¦  4  ¦  3  ¦  2  ¦  1  ¦  0  ¦
¦-------+-----+-----+-----+-----+-----+-----+-----+-----¦
¦ VALUE ¦ 128 ¦ 64  ¦ 32  ¦ 16  ¦  8  ¦  4  ¦  2  ¦  1  ¦
+-------------------------------------------------------+

+------------------------------------------------------------------------+
¦ OCTET ¦ DEC ¦ 128 ¦ 64  ¦ 32  ¦ 16  ¦  8  ¦  4  ¦  2  ¦  1  ¦  BINARY  ¦
¦-------+-----+-----+-----+-----+-----+-----+-----+-----+-----+----------¦
¦   1   ¦ 198 ¦  1  ¦  1  ¦  0  ¦  0  ¦  0  ¦  1  ¦  1  ¦  0  ¦ 11000110 ¦
¦   2   ¦  51 ¦  0  ¦  0  ¦  1  ¦  1  ¦  0  ¦  0  ¦  1  ¦  1  ¦ 00110011 ¦
¦   3   ¦ 100 ¦  0  ¦  1  ¦  1  ¦  0  ¦  0  ¦  1  ¦  0  ¦  0  ¦ 01100100 ¦
¦   4   ¦ 223 ¦  1  ¦  1  ¦  0  ¦  1  ¦  1  ¦  1  ¦  1  ¦  1  ¦ 11011111 ¦
+------------------------------------------------------------------------+

+------------------------------------------------------------------------+
¦ OCTET ¦  BINARY  ¦   7 ¦   6 ¦   5 ¦   4 ¦   3 ¦   2 ¦   1 ¦   0 ¦ DEC ¦
¦-------+----------+-----+-----+-----+-----+-----+-----+-----+-----+-----¦
¦   1   ¦ 11000110 ¦ 128 ¦  64 ¦   0 ¦   0 ¦   0 ¦   4 ¦   2 ¦   0 ¦ 198 ¦
¦   2   ¦ 00110011 ¦   0 ¦   0 ¦  32 ¦  16 ¦   0 ¦   0 ¦   2 ¦   1 ¦  51 ¦
¦   3   ¦ 01100100 ¦   0 ¦  64 ¦  32 ¦   0 ¦   0 ¦   4 ¦   0 ¦   0 ¦ 100 ¦
¦   4   ¦ 11011111 ¦ 128 ¦  64 ¦   0 ¦  16 ¦   8 ¦   4 ¦   2 ¦   1 ¦ 223 ¦
+------------------------------------------------------------------------+

Remember the Truth Tables from school (in binary math, 0 is False, and 1 is True).

+--------+                +--------+
¦ RESULT ¦                ¦ RESULT ¦
+---------------------+--------¦  +-------------+--------¦
¦ False ¦ AND ¦ False ¦ FALSE  ¦  ¦ 0 ¦ AND ¦ 0 ¦ 0      ¦
¦ False ¦ AND ¦ True  ¦ FALSE  ¦  ¦ 0 ¦ AND ¦ 1 ¦ 0      ¦
¦ True  ¦ AND ¦ False ¦ FALSE  ¦  ¦ 1 ¦ AND ¦ 0 ¦ 0      ¦
¦ True  ¦ AND ¦ True  ¦ TRUE   ¦  ¦ 1 ¦ AND ¦ 1 ¦ 1      ¦
+------------------------------+  +----------------------+

+--------+                +--------+
¦ RESULT ¦                ¦ RESULT ¦
+---------------------+--------¦  +-------------+--------¦
¦ False ¦ OR  ¦ False ¦ FALSE  ¦  ¦ 0 ¦ OR  ¦ 0 ¦ 0      ¦
¦ False ¦ OR  ¦ True  ¦ TRUE   ¦  ¦ 0 ¦ OR  ¦ 1 ¦ 1      ¦
¦ True  ¦ OR  ¦ False ¦ TRUE   ¦  ¦ 1 ¦ OR  ¦ 0 ¦ 1      ¦
¦ True  ¦ OR  ¦ True  ¦ TRUE   ¦  ¦ 1 ¦ OR  ¦ 1 ¦ 1      ¦
+------------------------------+  +----------------------+

+--------+                +--------+
¦ RESULT ¦                ¦ RESULT ¦
+-------------+--------¦      +---------+--------¦
¦ NOT ¦ False ¦ TRUE   ¦      ¦ NOT ¦ 0 ¦ 1      ¦
¦ NOT ¦ True  ¦ FALSE  ¦      ¦ NOT ¦ 1 ¦ 0      ¦
+----------------------+      +------------------+

*If you perform IPv4 math for many years, you may get to the point where you can perform decimal/binary conversions in your head, and you can appear to be able to do IPv4 math in decimal. Even if you can do this in your head, always double-check with an IP calculator, or convert to binary and perform the math, before committing a change to a production network.

The IPv4 dotted-decimal notation, e.g., 198.51.100.223, is simply to make it easier for humans to read an address. The four separate sections, called octets, really have no meaning to IPv4. Do not make the common mistake of thinking the octets have a special meaning. An address is really a 32-bit binary number, and that is how network devices see and use an IPv4 address.

The example dotted-decimal address, 198.51.100.223, is binary 11000110001100110110010011011111 to a device on the network. You can see that the dotted-decimal representation really does make it easier for humans. Each octet is eight bits of the 32-bit address (hence the commonly used term, “octet”), so there are four octets (32 address bits / 8 bits per octet = 4 octets). The example 32-bit binary address is separated into four octets, then each binary octet is converted to a decimal number*.

+-----------------------------------------------------+
¦ OCTET   ¦        1 ¦        2 ¦        3 ¦        4 ¦
¦---------+----------+----------+----------+----------¦
¦ BINARY  ¦ 11000110 ¦ 00110011 ¦ 01100100 ¦ 11011111 ¦
¦ DECIMAL ¦      198 ¦       51 ¦      100 ¦      223 ¦
+-----------------------------------------------------+

Because each octet is eight bits in length, each octet will have a value between 0 and 255 (any values greater than 255 are invalid). The reason is that 28 = 256: 2 (the binary number base) to the power of 8 (eight bits per octet) equals 256, the number of different values that can be expressed by an eight-bit octet. Remember that the first value is 0, so the 256th value will be one less that the total number of values that can be expressed (256 – 1 = 255).

To correctly perform IPv4 math, you must do it in binary, otherwise you will make mistakes that will cause you problems and frustration. That means that you must convert the dotted-decimal notation to binary before trying to manipulate it.

Dotted-decimal: 198.51.100.223

+-----------------------------------------------------+
¦ OCTET   ¦        1 ¦        2 ¦        3 ¦        4 ¦
¦---------+----------+----------+----------+----------¦
¦ DECIMAL ¦      198 ¦       51 ¦      100 ¦      223 ¦
¦ BINARY  ¦ 11000110 ¦ 00110011 ¦ 01100100 ¦ 11011111 ¦
+-----------------------------------------------------+

*Leading zeroes in a dotted-decimal IPv4 address may be interpreted by some applications and programming languages as octal (base 8) rather than decimal (base 10), causing errors, and leading zeros should be avoided for the dotted-decimal IPv4 representation, but leading zeroes are necessary for the binary address octets because they represent bit positions in the full address, and leaving out a bit position will shorten the address and change the binary value.

A network mask is used to divide an address into two parts: Network and Host. Because IPv4 addresses are fixed 32-bits in length, a larger Network means a smaller Host, and vice versa. The division can be at any bit number, so it may fall within an octet, not on an octet boundary (as many people incorrectly assume it always does). A network mask is the same size as an address (32 bits), and it is expressed in dotted-decimal notation the same way you would express an address in dotted-decimal notation (four 8-bit octets, separated by a period). For example, 255.255.248.0.

A network mask consists of consecutive 1 bits (representing the Network), followed by the number of 0 bits (representing the Host) to total 32 bits (the address length). The number of 1 bits plus the number of 0 bits totals 32, the number of bits in an IPv4 address or network mask. For example, a network mask of 255.255.248.0.

+--------------------------------------------------------+
¦ OCTET   ¦        1 ¦        2 ¦           3 ¦        4 ¦
¦---------+----------+----------+-------------+----------¦
¦ DECIMAL ¦      255 ¦      255 ¦         248 ¦        0 ¦
¦---------+----------+----------+-------------+----------¦
¦ BINARY  ¦ 11111111 ¦ 11111111 ¦ 11111 ¦ 000 ¦ 00000000 ¦
¦---------+-----------------------------+----------------¦
¦ # BITS  ¦         21 Network          ¦    11 Host     ¦
+--------------------------------------------------------+

As you can see, the division between Network and Host of the address using this mask falls within an octet, not on an octet boundary.

A network mask is often represented by the number of consecutive 1 bits in the mask. This is variously called the network mask length or prefix length, and it is represented as a / followed by the number of consecutive 1 bits in the network mask. Counting the number of consecutive 1 bits in the example totals 21, which can be represented as /21.

Given a mask length, you can calculate the dotted-decimal representation of the mask. Simply put down the number of 1 bits for the mask length and add enough 0 bits on the end to total 32 bits. Convert the resulting binary number into the dotted-decimal representation.

+--------------------------------------------------------+
¦ # BITS  ¦         21 Network          ¦    11 Host     ¦
¦---------+-----------------------------+----------------¦
¦ BINARY  ¦ 11111111 ¦ 11111111 ¦ 11111 ¦ 000 ¦ 00000000 ¦
¦---------+----------+----------+-------------+----------¦
¦ DECIMAL ¦      255 ¦      255 ¦         248 ¦        0 ¦
¦---------+----------+----------+-------------+----------¦
¦ OCTET   ¦        1 ¦        2 ¦           3 ¦        4 ¦
+--------------------------------------------------------+

The example 198.51.100.223 address may be represented traditionally with the example network mask as 198.51.100.223 255.255.248.0, or it may be represented in the more modern CIDR (Classless Inter-Domain Routing) notation as 198.51.100.223/21. Either representation is valid, and you can easily convert between the mask and the mask length as required (OSes and applications will require a specific representation).

A network address is an address with all Host bits set to 0. The network address can be calculated by a bitwise AND of the respective bits in the binary representation of the address and the network mask. Align the bits, perform a bitwise AND on each pair of the respective bits, then convert the individual octets of the result back to decimal.

+-------------------------------------------------------------+
¦ OCTET           ¦        1 ¦        2 ¦        3 ¦        4 ¦
¦-----------------+----------+----------+----------+----------¦
¦ BINARY ADDRESS  ¦ 11000110 ¦ 00110011 ¦ 01100100 ¦ 11011111 ¦
¦ BINARY MASK     ¦ 11111111 ¦ 11111111 ¦ 11111000 ¦ 00000000 ¦
¦ BITWISE AND     ¦ 11000110 ¦ 00110011 ¦ 01100000 ¦ 00000000 ¦
¦-----------------+----------+----------+----------+----------¦
¦ DECIMAL NETWORK ¦      198 ¦       51 ¦       96 ¦        0 ¦
+-------------------------------------------------------------+

The network address of 198.51.100.223/21 is 198.51.96.0. Notice that you cannot depend on the octets to distinguish between Network and Host.

+-------------------------------------------------------------+
¦ OCTET           ¦        1 ¦        2 ¦        3 ¦        4 ¦
¦-----------------+----------+----------+----------+----------¦
¦ BINARY ADDRESS  ¦ 11000110 ¦ 00110011 ¦ 01100110 ¦ 00111001 ¦
¦ BINARY MASK     ¦ 11111111 ¦ 11111111 ¦ 11111000 ¦ 00000000 ¦
¦ BITWISE AND     ¦ 11000110 ¦ 00110011 ¦ 01100000 ¦ 00000000 ¦
¦-----------------+----------+----------+----------+----------¦
¦ DECIMAL NETWORK ¦      198 ¦       51 ¦       96 ¦        0 ¦
+-------------------------------------------------------------+

Compare the target network address to the example network address, and notice that the network addresses are equal, meaning the example and target addresses are on the same network.

+-------------------------------------------------------------+
¦ OCTET           ¦        1 ¦        2 ¦        3 ¦        4 ¦
¦-----------------+----------+----------+----------+----------¦
¦ BINARY ADDRESS  ¦ 01001010 ¦ 01111101 ¦ 01000101 ¦ 01100100 ¦
¦ BINARY MASK     ¦ 11111111 ¦ 11111111 ¦ 11111000 ¦ 00000000 ¦
¦ BITWISE AND     ¦ 01001010 ¦ 01111101 ¦ 01000000 ¦ 00000000 ¦
¦-----------------+----------+----------+----------+----------¦
¦ DECIMAL NETWORK ¦       74 ¦      125 ¦       64 ¦        0 ¦
+-------------------------------------------------------------+

Compare the target network address to the example network address, and notice that the network addresses are different, meaning the example and target addresses are not on the same network.

*This is the method a source uses to determine if a destination is on the same network as the source. Packets destined to a different network must be sent to a router for forwarding to a different network.

One useful, often overlooked, value for IPv4 addressing is the host mask. A host mask is simply the inverse (bitwise NOT) of the network mask.

+-----------------------------------------------------------------+
¦ OCTET #             ¦        1 ¦        2 ¦        3 ¦        4 ¦
¦---------------------+----------+----------+----------+----------¦
¦ BINARY NETWORK MASK ¦ 11111111 ¦ 11111111 ¦ 11111000 ¦ 00000000 ¦
¦ BITWISE NOT         ¦ 00000000 ¦ 00000000 ¦ 00000111 ¦ 11111111 ¦
¦---------------------+----------+----------+----------+----------¦
¦ HOST MASK           ¦        0 ¦        0 ¦        7 ¦      255 ¦
+-----------------------------------------------------------------+

+--------------------------------------------------------------+
¦ OCTET #          ¦        1 ¦        2 ¦        3 ¦        4 ¦
¦------------------+----------+----------+----------+----------¦
¦ BINARY HOST MASK ¦ 00000000 ¦ 00000000 ¦ 00000111 ¦ 11111111 ¦
¦ BITWISE NOT      ¦ 11111111 ¦ 11111111 ¦ 11111000 ¦ 00000000 ¦
¦------------------+----------+----------+----------+----------¦
¦ NETWORK MASK     ¦      255 ¦      255 ¦      248 ¦        0 ¦
+--------------------------------------------------------------+

+-----------------------------------------------------------------+
¦ OCTET #             ¦        1 ¦        2 ¦        3 ¦        4 ¦
¦---------------------+----------+----------+----------+----------¦
¦ BINARY /32 MASK     ¦ 11111111 ¦ 11111111 ¦ 11111111 ¦ 11111111 ¦
¦ BINARY NETWORK MASK ¦ 11111111 ¦ 11111111 ¦ 11111000 ¦ 00000000 ¦
¦ SUBTRACTION         ¦ 00000000 ¦ 00000000 ¦ 00000111 ¦ 11111111 ¦
¦---------------------+----------+----------+----------+----------¦
¦ HOST MASK           ¦        0 ¦        0 ¦        7 ¦      255 ¦
+-----------------------------------------------------------------+

+--------------------------------------------------------------+
¦ OCTET #          ¦        1 ¦        2 ¦        3 ¦        4 ¦
¦------------------+----------+----------+----------+----------¦
¦ BINARY /32 MASK  ¦ 11111111 ¦ 11111111 ¦ 11111111 ¦ 11111111 ¦
¦ BINARY HOST MASK ¦ 00000000 ¦ 00000000 ¦ 00000111 ¦ 11111111 ¦
¦ SUBTRACTION      ¦ 11111111 ¦ 11111111 ¦ 11111000 ¦ 00000000 ¦
¦------------------+----------+----------+----------+----------¦
¦ NETWORK MASK     ¦      255 ¦      255 ¦      248 ¦        0 ¦
+--------------------------------------------------------------+

+------------------------------------------+
¦ OCTET #          ¦   1 ¦   2 ¦   3 ¦   4 ¦
¦------------------+-----+-----+-----+-----¦
¦ DECIMAL /32 MASK ¦ 255 ¦ 255 ¦ 255 ¦ 255 ¦
¦ NETWORK MASK     ¦ 255 ¦ 255 ¦ 248 ¦   0 ¦
¦------------------+-----+-----+-----+-----¦
¦ HOST MASK        ¦   0 ¦   0 ¦   7 ¦ 255 ¦
+------------------------------------------+

+------------------------------------------+
¦ OCTET #          ¦   1 ¦   2 ¦   3 ¦   4 ¦
¦------------------+-----+-----+-----+-----¦
¦ DECIMAL /32 MASK ¦ 255 ¦ 255 ¦ 255 ¦ 255 ¦
¦ HOST MASK        ¦   0 ¦   0 ¦   7 ¦ 255 ¦
¦------------------+-----+-----+-----+-----¦
¦ NETWORK MASK     ¦ 255 ¦ 255 ¦ 248 ¦   0 ¦
+------------------------------------------+

+---------------------------------------------------------------+
¦ OCTET             ¦        1 ¦        2 ¦        3 ¦        4 ¦
¦-------------------+----------+----------+----------+----------¦
¦ BINARY ADDRESS    ¦ 11000110 ¦ 00110011 ¦ 01100100 ¦ 11011111 ¦
¦ BINARY HOST MASK  ¦ 00000000 ¦ 00000000 ¦ 00000111 ¦ 11111111 ¦
¦ BITWISE OR        ¦ 11000110 ¦ 00110011 ¦ 01100111 ¦ 11111111 ¦
¦-------------------+----------+----------+----------+----------¦
¦ BROADCAST ADDRESS ¦      198 ¦       51 ¦      103 ¦      255 ¦
+---------------------------------------------------------------+

You can simply add the value of the host mask to the value of the network address (not the host address) and you can do this either in decimal or binary.

Decimal

+-------------------------------------------+
¦ OCTET #           ¦   1 ¦   2 ¦   3 ¦   4 ¦
¦-------------------+-----+-----+-----+-----¦
¦ DECIMAL NETWORK   ¦ 198 ¦  51 ¦  96 ¦   0 ¦
¦ DECIMAL HOST MASK ¦   0 ¦   0 ¦   7 ¦ 255 ¦
¦-------------------+-----+-----+-----+-----¦
¦ BROADCAST ADDRESS ¦ 198 ¦  51 ¦ 103 ¦ 255 ¦
+-------------------------------------------+

Binary

+--------------------------------------------------------------+
¦ OCTET #          ¦        1 ¦        2 ¦        3 ¦        4 ¦
¦------------------+----------+----------+----------+----------¦
¦ BINARY NETWORK   ¦ 11000110 ¦ 00110011 ¦ 01100000 ¦ 00000000 ¦
¦ BINARY HOST MASK ¦ 00000000 ¦ 00000000 ¦ 00000111 ¦ 11111111 ¦
¦ ADDITION         ¦ 11000110 ¦ 00110011 ¦ 01100111 ¦ 11111111 ¦
¦------------------+----------+----------+----------+----------¦
¦ NETWORK MASK     ¦      198 ¦       51 ¦      103 ¦      255 ¦
+--------------------------------------------------------------+

The total number of host addresses for a network is 2 to the power of the number of host bits, which is 32 (IPv4 address bits) minus the number of network bits. For example, for a /21 (network mask 255.255.248.0) network, there are 11 host bits (32 address bits – 21 network bits = 11 host bits). That means there are 2048 total host addresses in a /21 network (211 = 2048).

Total Usable IPv4 Network Host Addresses

Except for /31 (255.255.255.254) and /32 (255.255.255.255) networks, the number of usable host addresses in a network is the total number of network host addresses minus 2 (because the network and broadcast addresses are unusable for host addresses on the network, you must subtract them from the number of usable host addresses). For example, in a /21 (255.255.248.0) network, there are 2046 usable host addresses (211 - 2 = 2046).

First Usable IPv4 Network Host Address

Except for /31 (255.255.255.254) and /32 (255.255.255.255) networks, the first usable network host address is the network address plus (either addition or bitwise OR) 1 (the network address is not usable for a network host address). For example, in the 198.51.96.0/21 network, the first usable network host address is 198.51.96.1 (198.51.96.0 + 1 = 198.51.96.1 or 198.51.96.0 OR 1 = 198.51.96.1). Set the low-order bit of the binary network address to 1.

+------------------------------------------------------------------+
¦ OCTET                ¦        1 ¦        2 ¦        3 ¦        4 ¦
¦----------------------+----------+----------+----------+----------¦
¦ BINARY NETWORK       ¦ 11000110 ¦ 00110011 ¦ 01100000 ¦ 00000000 ¦
¦ 1                    ¦ 00000000 ¦ 00000000 ¦ 00000000 ¦ 00000001 ¦
¦ ADD (OR)             ¦ 11000110 ¦ 00110011 ¦ 01100000 ¦ 00000001 ¦
¦----------------------+----------+----------+----------+----------¦
¦ FIRST USABLE ADDRESS ¦      198 ¦       51 ¦       96 ¦        1 ¦
+------------------------------------------------------------------+

Last Usable IPv4 Network Host Address

Except for /31 (255.255.255.254) and /32 (255.255.255.255) networks, the last usable network host address is the network broadcast address minus 1 (the network broadcast address is not usable for a network host address). For example, in the 198.61.96.0/21 network, the last usable network host address is 198.51.103.254 (198.51.103.255 - 1 = 198.51.103.254). Set the low-order bit of the binary IPv4 network broadcast address to 0.

+----------------------------------------------------------------------+
¦ OCTET                    ¦        1 ¦        2 ¦        3 ¦        4 ¦
¦--------------------------+----------+----------+----------+----------¦
¦ BINARY BROADCAST ADDRESS ¦ 11000110 ¦ 00110011 ¦ 01100111 ¦ 11111111 ¦
¦ 1                        ¦ 00000000 ¦ 00000000 ¦ 00000000 ¦ 00000001 ¦
¦ SUBTRACT                 ¦ 11000110 ¦ 00110011 ¦ 01100111 ¦ 11111110 ¦
¦--------------------------+----------+----------+----------+----------¦
¦ LAST  USABLE ADDRESS     ¦      198 ¦       51 ¦      103 ¦      254 ¦
+----------------------------------------------------------------------+

IPv4 /31 (255.255.255.254) Networks

Originally, /31 (255.255.255.254) networks were unusable because there is only one host bit, giving you two total network host addresses, but the number of usable network host addresses is the total number of network host addresses minus 2 (2 total host addresses - 2 = 0 usable host addresses).

Point-to-point links only need two host addresses (one for each end of the link). The traditional way of assigning IPv4 networks required the use of /30 (255.255.255.252) networks for point-to-point links, but that wastes half the network host addresses because a /30 network has four total network host addresses, but only two are usable network host addresses (22 – 2 = 2).

With the critical IPv4 address shortage, a standard was created (RFC 3021, Using 31-Bit Prefixes on IPv4 Point-to-Point Links) to allow the use of /31 networks for point-to-point links. That makes sense because there is no need for broadcast on such networks: any packets sent by a host on the network are destined for the only other host on the network, effectively broadcasting. In a /31 network, the network address is the first usable host address, and the broadcast address is the last usable host address.

Unfortunately, not all vendors (Microsoft in particular) support the standard for using /31 networks on point-to-point links, and you will most often see point-to-point links using /30 networks.

IPv4 /32 (255.255.255.255) Networks

A /32 (255.255.255.255) network is both a network with no host addresses, and a host address, itself. There is only one address in the network, and that is the network address. Because there are no other hosts are on the network, traffic must be routed to and from the network address.

These addresses are often used on virtual network interfaces defined inside a device that can route packets between its virtual and physical interfaces. An example of this is to create a virtual interface in a network device to be used as the source or destination for the device itself. A virtual interface cannot drop because of a physical problem, e.g., cable unplugged, and if the device has multiple paths into it, other devices can still communicate with the device using the virtual interface address when a physical interface of the device is inoperable for some reason.

Putting IPv4 Network Addressing All Together

For example, the network address 198.51.100.223 and mask 255.255.248.0 (or 198.51.100.223/21), we can calculate the network information.*

+--------------------------------------------+
¦ HOST ADDRESS              ¦ 198.51.100.223 ¦
¦ NETWORK MASK              ¦  255.255.248.0 ¦
¦ NETWORK MASK LENGTH       ¦             21 ¦
¦ HOST MASK                 ¦      0.0.7.255 ¦
¦ HOST MASK LENGTH          ¦             11 ¦
¦ NETWORK ADDRESS           ¦    198.51.96.0 ¦
¦ FIRST USABLE HOST ADDRESS ¦    198.51.96.1 ¦
¦ LAST USABLE HOST ADDRESS  ¦ 198.51.103.254 ¦
¦ TOTAL HOST ADDRESSES      ¦           2048 ¦
¦ USABLE HOST ADDRESSES     ¦           2046 ¦
+--------------------------------------------+

*Network education class exams and certification tests will ask you to be able to quickly calculate these values, given a host address and mask (or mask length). You can use the hints below for a quick check of your answers:

• Network Address (hint: an even number)
• First Usable Host Address (hint: Network Address plus 1, an odd number)

The above hints do not apply to /31 (255.255.255.254) or /32 (255.255.255.255) networks.

Given enough time on your exam, and a problem that has multiple methods to arrive at an answer, you should use the multiple methods to double-check the answer.

Continued in Part 2...

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).

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:

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

• 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. Jun 4, 2015 at 7:34

Continued from Part 1...

Part 2 of 2

Subnet IPv4 Networks

Subnetting a network is creating multiple, longer networks from a network address and mask. The basic idea is that you borrow high-order bits from the Host of the original network address, extending the Network.

Assume you want to create 14 equal-sized subnets from the original 198.51.96.0/21 network address. Since you are borrowing high-order bits from the Host of the original Network, you will get a number that is a power of 2, but 14 is not a power of 2, so you must get the next higher power of 2, which happens to be 16 (16 = 24). The power of 2, in this case 4, is the number of bits necessary to borrow from the Host for the number of subnets to be created. You can use a mathematical formula to determine the number of bits required: Log2(X subnets) = Y borrowed bits, rounded up to the next integer value.

Log2(14 subnets) = 3.807354922, rounded up = 4 borrowed bits

For example, creating 14 equal-sized subnets from the original 198.51.96.0/21 network, starting with 0s* for the first subnet, add 1 to the subnet portion to get the next subnet.

+-------------------------------------------------------------------------------+
¦ ORIGINAL BITS    ¦      21 Network       ¦    11 Host     ¦                   ¦
¦------------------+-----------------------+----------------+-------------------¦
¦ ORIGINAL NETWORK ¦ 110001100011001101100 ¦ 00000000000    ¦ 198.51.96.0/21    ¦
¦------------------+----------------------------------------+-------------------¦
¦ BORROW 4 BITS    ¦          25 Network          ¦ 7 Host  ¦                   ¦
¦------------------+------------------------------+---------+-------------------¦
¦ SUBNET 1 /25     ¦ 110001100011001101100 ¦ 0000 ¦ 0000000 ¦ 198.51.96.0/25    ¦
¦------------------+-----------------------+------+---------+-------------------¦
¦ SUBNET 2 /25     ¦ 110001100011001101100 ¦ 0001 ¦ 0000000 ¦ 198.51.96.128/25  ¦
¦------------------+-----------------------+------+---------+-------------------¦
¦ SUBNET 3 /25     ¦ 110001100011001101100 ¦ 0010 ¦ 0000000 ¦ 198.51.97.0/25    ¦
¦------------------+-----------------------+------+---------+-------------------¦
¦ SUBNET 4 /25     ¦ 110001100011001101100 ¦ 0011 ¦ 0000000 ¦ 198.51.97.128/25  ¦
¦------------------+-----------------------+------+---------+-------------------¦
¦ SUBNET 5 /25     ¦ 110001100011001101100 ¦ 0100 ¦ 0000000 ¦ 198.51.98.0/25    ¦
¦------------------+-----------------------+------+---------+-------------------¦
¦ SUBNET 6 /25     ¦ 110001100011001101100 ¦ 0101 ¦ 0000000 ¦ 198.51.98.128/25  ¦
¦------------------+-----------------------+------+---------+-------------------¦
¦ SUBNET 7 /25     ¦ 110001100011001101100 ¦ 0110 ¦ 0000000 ¦ 198.51.99.0/25    ¦
¦------------------+-----------------------+------+---------+-------------------¦
¦ SUBNET 8 /25     ¦ 110001100011001101100 ¦ 0111 ¦ 0000000 ¦ 198.51.99.128/25  ¦
¦------------------+-----------------------+------+---------+-------------------¦
¦ SUBNET 9 /25     ¦ 110001100011001101100 ¦ 1000 ¦ 0000000 ¦ 198.51.100.0/25   ¦
¦------------------+-----------------------+------+---------+-------------------¦
¦ SUBNET 10 /25    ¦ 110001100011001101100 ¦ 1001 ¦ 0000000 ¦ 198.51.100.128/25 ¦
¦------------------+-----------------------+------+---------+-------------------¦
¦ SUBNET 11 /25    ¦ 110001100011001101100 ¦ 1010 ¦ 0000000 ¦ 198.51.101.0/25   ¦
¦------------------+-----------------------+------+---------+-------------------¦
¦ SUBNET 12 /25    ¦ 110001100011001101100 ¦ 1011 ¦ 0000000 ¦ 198.51.101.128/25 ¦
¦------------------+-----------------------+------+---------+-------------------¦
¦ SUBNET 13 /25    ¦ 110001100011001101100 ¦ 1100 ¦ 0000000 ¦ 198.51.102.0/25   ¦
¦------------------+-----------------------+------+---------+-------------------¦
¦ SUBNET 14 /25    ¦ 110001100011001101100 ¦ 1101 ¦ 0000000 ¦ 198.51.102.128/25 ¦
¦------------------+----------------------------------------+-------------------¦
¦ LEFTOVER         ¦         24 Network          ¦  8 Host  ¦                   ¦
¦------------------+-----------------------------+----------+-------------------¦
¦ UNUSED 1 /24     ¦ 110001100011001101100 ¦ 111 ¦ 00000000 ¦ 198.51.103.0/24   ¦
+-------------------------------------------------------------------------------+

It is possible to subnet a network address into variously sized subnets (every network address, regardless of size, is a subnet of the 0.0.0.0/0 network), as in our example above, where the unused addressing is a /24 subnet, but this requires careful planning so that the resulting subnets start on the correct bit for the subnet size.

For example, let's say that we need both a /26 and a /27 subnet from the 198.51.96.0/21 network. There are two ways to do that: start with the /26 subnet or start with the /27 subnet.

Starting with the /26 subnet:

+--------------------------------------------------------------------------------+
¦ ORIGINAL BITS    ¦      21 Network       ¦   11 Host       ¦                   ¦
¦------------------+-----------------------+-----------------+-------------------¦
¦ ORIGINAL NETWORK ¦ 110001100011001101100 ¦ 00000000000     ¦ 198.51.96.0/21    ¦
¦------------------+-----------------------------------------+-------------------¦
¦ BORROW 5 BITS    ¦      26 Network               ¦ 6 Host  ¦                   ¦
¦------------------+-------------------------------+---------+-------------------¦
¦ SUBNET /26       ¦ 110001100011001101100 ¦ 00000 ¦ 000000  ¦ 198.51.96.0/26    ¦
¦------------------+-----------------------------------------+-------------------¦
¦ BORROW 1 BIT     ¦      27 Network                ¦ 5 Host ¦                   ¦
¦------------------+--------------------------------+--------+-------------------¦
¦ SUBNET /27       ¦ 110001100011001101100 ¦ 000010 ¦ 00000  ¦ 198.51.96.64/27   ¦
+--------------------------------------------------------------------------------+

Notice that the second /26 subnet is where the /27 subnet is defined, and that works well because 27 is larger than 26.

Starting with the /27 subnet:

+--------------------------------------------------------------------------------+
¦ ORIGINAL BITS    ¦      21 Network       ¦   11 Host       ¦                   ¦
¦------------------+-----------------------+-----------------+-------------------¦
¦ ORIGINAL NETWORK ¦ 110001100011001101100 ¦ 00000000000     ¦ 198.51.96.0/21    ¦
¦------------------+-----------------------------------------+-------------------¦
¦ BORROW 6 BITS    ¦      27 Network                ¦ 5 Host ¦                   ¦
¦------------------+--------------------------------+--------+-------------------¦
¦ SUBNET /27       ¦ 110001100011001101100 ¦ 000000 ¦ 00000  ¦ 198.51.96.0/27    ¦
¦------------------+-----------------------+--------+--------+-------------------¦
¦ UNUSED /27       ¦ 110001100011001101100 ¦ 000001 ¦ 00000  ¦ 198.51.96.32/27   ¦
¦------------------+-----------------------------------------+-------------------¦
¦ RETURN 1 BIT     ¦      26 Network               ¦ 6 Host  ¦                   ¦
¦------------------+-------------------------------+---------+-------------------¦
¦ SUBNET /26       ¦ 110001100011001101100 ¦ 00001 ¦ 000000  ¦ 198.51.96.64/26   ¦
+--------------------------------------------------------------------------------+

Notice that there are not enough bits left in the host portion (five host bits) in the next unused addressing to support a /26 subnet, which requires six host bits (32 address bits - 26 network bits = 6 host bits). If we use that as the starting position for the /26 subnet, we will overlap the previous and next /26 subnets. We need to leave a gap the size of a /27 subnet for the starting position of the /26 subnet.

A /26 subnet must always start on a /26 boundary: every 2nd /27 subnet boundary, every 4th /28 subnet boundary, every 8th /29 subnet boundary, etc. This rule is for any subnet size: a subnet must start on a boundary of a longer subnet that is equal to 2 to the power of the longer subnet size minus the subnet size. For example, a /23 subnet must start on every 4th /25 network (2(25 - 23) = 22 = 4).

Trying to configure a device with a network address that starts on the wrong bit boundary will either lead to strange, hard to troubleshoot problems, or the device will give you an error about overlapping networks. Some people try to do this with dotted-decimal, and this will lead to errors. For example, the 198.51.96.0/27 network address range is 198.51.96.0 through 198.51.96.31. If you know that and try to use the 198.51.96.32/26 network, you will run into problems because that network starts on the wrong bit boundary and overlaps the /27 network (check by using a bitwise AND with the address and the network mask: 198.51.96.32 AND 255.255.255.192 = 198.51.96.0, the same network address as the /27 network). It is obvious in binary, but it is not so obvious in dotted-decimal. You can learn that /26 networks must start on a multiple of decimal 64 boundary, but seeing it in binary can tell you for sure whether or not you made a mistake.

*There is a persistent myth that for subnets, as for host addresses, the all-zeros and all-ones subnets cannot be used, but this myth was dispelled many years ago with the deprecation of classful networking. Unfortunately, this myth extends to some network educations courses, and the correct answer for those (incorrect) courses would be to use the 2nd through 15th subnets.

Subnet Size Based on Number of Hosts

Common exam questions will give you a network and ask you to come up with several variously-sized subnets based on the number of hosts for each subnet. If you can, clarify if the number of hosts is based on the total number of host addresses on the network, or if it is based on the number of usable hosts on the network. For example, if the question asks for a subnet with 256 or 255 hosts, a /24 network will give you 256 total host addresses, but only 254 usable host addresses. Such a question may be a trick question, and the correct answer will hinge on whether the question means total host addresses or usable host addresses.

Given the 198.51.96.0/21 network, subnet it for the following departments:

• Department 1: 500 hosts
• Department 2: 100 hosts
• Department 3: 200 hosts
• Department 4: 1000 hosts

As we saw in the Subnet IPv4 Networks section, the easiest way to do this is to first sort the departments by the largest to smallest number of hosts to avoid dealing with network gaps that could require more addressing than is available in the given starting network.

• Department 4: 1000 hosts
• Department 1: 500 hosts
• Department 3: 200 hosts
• Department 2: 100 hosts

You can round each up to the next high power of 2 to get the number of required total host addresses for each subnet, then derive the number of required host bits from the exponent of the power of 2.

• Department 4: 1024 total hosts = 210 = 10 host bits
• Department 1: 512 total hosts = 29 = 9 host bits
• Department 3: 256 total hosts = 28 = 8 host bits
• Department 2: 128 total hosts = 27 = 7 host bits

You can also use the formula for finding the number bits required for a subnet to determine the number of host bits required for each subnet: Log2(X hosts) = Y host bits, rounded up to the next integer value.

• Department 4: Log2(1000 hosts) = 9.96578428466209, rounded up = 10 host bits
• Department 1: Log2( 500 hosts) = 8.96578428466209, rounded up = 9 host bits
• Department 3: Log2( 200 hosts) = 7.64385618977472, rounded up = 8 host bits
• Department 2: Log2( 100 hosts) = 6.64385618977473, rounded up = 7 host bits

Once you have the number of host bits required for each subnet, then perform the binary math to get the specific subnet for each department. Remember to add 1 to a subnet to get the starting address of the next subnet.

+-------------------------------------------------------------------------------+
¦ ORIGINAL BITS    ¦      21 Network       ¦   11 Host      ¦                   ¦
¦------------------+-----------------------+----------------+-------------------¦
¦ ORIGINAL NETWORK ¦ 110001100011001101100 ¦ 00000000000    ¦ 198.51.96.0/21    ¦
¦------------------+----------------------------------------+-------------------¦
¦ BORROW 1 BIT     ¦      22 Network           ¦  10 Host   ¦                   ¦
¦------------------+---------------------------+------------+-------------------¦
¦ DEPARTMENT 4     ¦ 110001100011001101100 ¦ 0 | 0000000000 ¦ 198.51.96.0/22    ¦
¦------------------+----------------------------------------+-------------------¦
¦ BORROW 1 BIT     ¦      23 Network            ¦  9 Host   ¦                   ¦
¦------------------+----------------------------+-----------+-------------------¦
¦ DEPARTMENT 1     ¦ 110001100011001101100 ¦ 10 ¦ 000000000 ¦ 198.51.100.0/23   ¦
¦------------------+----------------------------------------+-------------------¦
¦ BORROW 1 BIT     ¦      24 Network             ¦  8 Host  ¦                   ¦
¦------------------+-----------------------------+----------+-------------------¦
¦ DEPARTMENT 3     ¦ 110001100011001101100 ¦ 110 ¦ 00000000 ¦ 198.51.102.0/24   ¦
¦------------------+----------------------------------------+-------------------¦
¦ BORROW 1 BIT     ¦      25 Network              ¦ 7 Host  ¦                   ¦
¦------------------+------------------------------+---------+-------------------¦
¦ DEPARTMENT 2     ¦ 110001100011001101100 ¦ 1110 ¦ 0000000 ¦ 198.51.103.0/25   ¦
¦------------------+-----------------------+------+---------+-------------------¦
¦ UNUSED           ¦ 110001100011001101100 ¦ 1111 ¦ 0000000 ¦ 198.51.103.128/25 ¦
+-------------------------------------------------------------------------------+

Find a Particular Subnet

You may be asked to give the network information for a particular subnet of a given network address. For example, you may be asked to give the network information for the 23rd /26 subnet of the 198.51.96.0/21 network address. Since you need the 23rd subnet, you convert decimal 22 (remember 0 is the first subnet, so the 23rd subnet would be 22*) to binary 10110. Use the converted binary number in the subnet portion of the address.

+--------------------------------------------------------------------------------+
¦ ORIGINAL BITS    ¦      21 Network       ¦   11 Host       ¦                   ¦
¦------------------+-----------------------+-----------------+-------------------¦
¦ ORIGINAL NETWORK ¦ 110001100011001101100 ¦ 00000000000     ¦ 198.51.96.0/21    ¦
¦------------------+-----------------------------------------+-------------------¦
¦ BORROW 5 BITS    ¦      26 Network               ¦ 6 Host  ¦                   ¦
¦------------------+-------------------------------+---------+-------------------¦
¦ SUBNET 23 /26    ¦ 110001100011001101100 ¦ 10110 ¦ 000000  ¦ 198.51.101.128/26 ¦
+--------------------------------------------------------------------------------+

Once you have identified the 23rd network address, 198.51.101.128/26, you can calculate the other network information (as described in the previous sections).

+---------------------------------------------+
¦ NETWORK ADDRESS           ¦  198.51.101.128 ¦
¦ NETWORK MASK              ¦ 255.255.255.192 ¦
¦ NETWORK MASK LENGTH       ¦              26 ¦
¦ HOST MASK                 ¦        0.0.0.63 ¦
¦ HOST MASK LENGTH          ¦               6 ¦
¦ FIRST USABLE HOST ADDRESS ¦  198.51.101.129 ¦
¦ LAST USABLE HOST ADDRESS  ¦  198.51.101.190 ¦
¦ TOTAL HOST ADDRESSES      ¦              64 ¦
¦ USABLE HOST ADDRESSES     ¦              62 ¦
+---------------------------------------------+

*There is a persistent myth that for subnets, as for host addresses, the all-zeros and all-ones subnets cannot be used, but this myth was explicitly dispelled many years ago with the deprecation of classful networking. Unfortunately, this myth extends to some network educations classes, and the correct answer for those (incorrect) classes would be to use the 24th (23 decimal, 10111 binary) subnet in our example of equal-sized subnets, rather than the actual 23rd (22 decimal, 10110 binary) subnet.

Find a Particular Network Host

You may be asked to find the host address for a particular host of a given network. For example, you may be asked to give the host address for the 923rd host of the 198.51.96.0/21 network. Since you need the 923rd host, you can convert 923 to binary 1110011011. Add the converted binary number to the network address:

+--------------------------------------------------------------------------------+
¦ ORIGINAL BITS    ¦      21 Network       ¦   11 Host       ¦                   ¦
¦------------------+-----------------------+-----------------+-------------------¦
¦ ORIGINAL NETWORK ¦ 110001100011001101100 ¦ 00000000000     ¦ 198.51.96.0/21    ¦
¦------------------+-----------------------+-----------------+-------------------¦
¦ ADD 923          ¦ 000000000000000000000 ¦ 01110011011     ¦                   ¦
¦------------------+-----------------------+-----------------+-------------------¦
¦ HOST 923         ¦ 110001100011001101100 ¦ 01110011011     ¦ 198.51.99.155/21  ¦
+--------------------------------------------------------------------------------+

You may be given two (or more) different addresses and asked to come up with the largest Network (smallest Host) that contains the addresses. For example, find the largest common network address of 198.51.100.223 and 198.51.101.76.

Convert the addresses to binary. Starting from the highest-order (leftmost) bit, compare the binary addresses at each bit position until the bits in the same position do not match.

+----------------------------------------------------------------+
¦ OCTET           ¦        1 ¦        2 ¦           3 ¦        4 ¦
¦-----------------+----------+----------+-------------+----------¦
¦ 198.51.100.223  ¦ 11000110 ¦ 00110011 ¦ 0110010 ¦ 0 ¦ 11011111 ¦
¦ 198.51.101.76   ¦ 11000110 ¦ 00110011 ¦ 0110010 ¦ 1 ¦ 01001100 ¦
¦-----------------+----------+----------+---------+---+----------¦
¦ MASK /23        ¦ 11111111 ¦ 11111111 ¦ 1111111 ¦ 0 ¦ 00000000 ¦
¦-----------------+----------+----------+---------+---+----------¦
¦ 198.51.100.0/23 ¦ 11000110 ¦ 00110011 ¦ 0110010 ¦ 0 ¦ 00000000 ¦
+----------------------------------------------------------------+

Count the number of matching bits, 23 in this case, to get the mask length. You can then take either address and perform a bitwise AND with the network mask to get the common network. Doing this on both addresses should result in the same network, and if it does not, then you either miscounted, or you missed an unmatched bit position.

You can use this same method for network aggregation (sometimes incorrectly called supernetting). This will work with network addresses the same way was with host addresses, but remember that it may encompass networks you do not control, so be careful when aggregating networks to advertise.

*You may see this called the smallest common network (or some variant, e.g., minimum network or mask). The smallest network is 0.0.0.0/0 (0 Network bits), and it is the smallest common network for all network and addresses. The confusion arises because many people look at the number of hosts in a network and confuse that number with the size of the Network. Remember that the larger the Network, the smaller the Host, and vice versa.

Select an IPv4 Network Gateway (Router) Address

A gateway is a host in the network that knows how to forward packets to other networks, and it can be assigned any usable host address. Some people just randomly assign gateway addresses to any usable network host address, some people always assign the first usable host address to a gateway, and some people always assign the last usable host address to a gateway. It does not matter which usable host address you assign to a gateway, but you should try to be consistent.

IPv4, itself, does not have the concept of, nor distinction between, public and private addressing. The IPv4 Private addressing was arbitrarily chosen, and the ISPs, by agreement, will not forward packets on the public Internet using addresses in the Private address space, but network devices and hosts have no idea if an address is public or private.

1. 10.0.0.0/8
2. 172.16.0.0/12
3. 192.168.0.0/16

You are free to use any or all the Private IP addressing in your own private network, but you cannot use any addresses in any of the three Private address ranges on the public Internet, and you must use a workaround, e.g. some variant of NAT, to use the public Internet from a privately addressed network.

Originally, IPv4 addresses were divided into network classes. Classful addressing was deprecated decades ago (in 1993, two years before the commercial Internet in 1995), and modern networking is based on CIDR (Classless Inter-Domain Routing), but, unfortunately, many network education course and certification exams insist on testing your knowledge of classful addressing. Please learn and be comfortable with all the previous IPv4 math and subnetting in this document before you learn about classful addressing.

The IPv4 address classes are all based on the first bits of the address:

+-------------------------------------------------------------------+
¦ CLASS ¦ START        ¦ BITS ¦ ADDRESS RANGE               ¦ SIZE* ¦
¦-------+--------------+------+-----------------------------+-------¦
¦   A   ¦ First 1 bit  ¦ 0    ¦   0.0.0.0 - 127.255.255.255 ¦   /8  ¦
¦   B   ¦ First 2 bits ¦ 10   ¦ 128.0.0.0 - 191.255.255.255 ¦  /16  ¦
¦   C   ¦ First 3 bits ¦ 110  ¦ 192.0.0.0 - 223.255.255.255 ¦  /24  ¦
¦   D   ¦ First 4 bits ¦ 1110 ¦ 224.0.0.0 - 239.255.255.255 ¦   -   ¦
¦   E   ¦ First 4 bits ¦ 1111 ¦ 240.0.0.0 - 255.255.255.255 ¦   -   ¦
+-------------------------------------------------------------------+

Class E addresses are reserved, and they cannot be used for anything. There is one exception to this, and that is the Limited Broadcast address of 255.255.255.255, which is an individual address that every host on a network will treat as its own. That means that anything sent to 255.255.255.255 will be received and processed by every host on the network.

Because each class has a default network size, some questions assume the default mask for a given address, so any calculations need to be made based on the default network mask. For example, the 198.51.100.223 address:

Binary: 11000110 00110011 01100100 11011111

Notice that the first three address bits are 110, meaning that this is a Class C address, and absent any mask or mask length, the network mask is assumed to be 255.255.255.0 (/24), making the network address 198.51.100.0.

*Do not make the common mistake of thinking the network mask dictates the network class, it is the other way around. For example, many people consider any /24 network to be a Class C network, but that is not true. Given, for example, a 10.11.12.0/24 network, many people incorrectly call that a Class C network because of the /24 network mask, even though the first bit of the address is 0, making it is a Class A network, albeit with a longer network mask than the default Class A network mask, meaning it is a subnet of a Class A network, not a Class C network. RFC 1166, Internet Numbers defines the network address classes.

Subnet Sizing Based on Number of Hosts

This is for the common question "How do I cut a given network size into n pieces allowing for x1 hosts in network 1, x2 hosts in network 2, etc ...?" can absolutely be solved by working through the methods described in the other excellent answers.

Some people however, might like a more visual method and some general tips.

Visual "Glasscutter" Method

The way I often teach a visual understanding of this is with the following method:

First imagine a paper guillotine like this:

(Picture from Wikipedia By Nathan CC BY-SA 3.0)

The properties of this kind of cutter are that it only cuts straight lines, it always cuts all the way across the paper, and it cuts perpendicular to a side. Our particular guillotine is fussy: it will only cut paper in half, and we can't make any cut closer than 1 cm from the edge.

• How many addresses are available in total for your starting block?
• Suppose were dividing up a /22 has 1024 addresses
• Get a piece of paper with that many square centimetres (and square or 2x1 ratio)
• Therefore I get a piece 32 cm by 32 cm which has 1024 sq cm
• Repeatedly
1. Choose a piece (if there's more than one)
2. Cut it in half (within constraints: only rectangular cuts, in half, nothing below 1 cm)
• Often there are different cuts you can make and you have to make a choice
• To get n networks, you need to make n-1 cuts
• Sometimes you end up with extra pieces (depending how you want to distribute the "waste")

Here's an illustration of the process. You see that there is only one kind of cut possible at cut 1 and cut 2, but at cut 3 we make a choice: cut the small piece (red) or the big piece (blue), giving two different possibilities.

The is what's often called the guillotine problem, which I learned as the "glasscutter" problem, as sheet glass really does has to be cut all the way across, and this specific might be called "binary glasscutter" as it's always cutting into halves.

When I actually do this in real life, I mentally do the halvings while looking at grid like this. I can remember that /26 must begin on 0, .64, 128 or .192; I might know that the seventh leased line needs the seventh /30 in the top quarter, but I won't remember that's .216.

The grid obviously can be used to represent the third octet too, and each square represents a /24. Now it says that a /18 begins on .0, .64, .128 or .192.

General Technique Tips

The general procedure is:

• round up each required size into the smallest block which is big enough
• make sure you follow whatever global rules (often "maximise the addressing available", sometimes it's "allow double for growth" or "make routing easy")
• allocate the subnets to addresses STARTING WITH THE BIGGEST and going down to the smallest (this is the part they usually forget to tell you)
• follow any specific rules (test questions often have extra rules, sometimes as abritrary as "No network address may have a 7 in it")
• if any network is small (/30, /31 or /32) pay extra attention as there are some edge cases for networks with 4, 2, and 1 hosts, and the details depend on what exact problem you're solving

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;
254 Host

Network=192.168.1.0 /25;