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?
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
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
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.
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...
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
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:
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 maximum netmask (minimum hostmask) 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.
With an IPv4 address and the network mask, the network mask length, or host mask, you can calculate the Network Address, Broadcast Address, Total Addresses, Usable Addresses, First Usable Address, and Last Usable Address.
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 ¦
+-------------------------------------------------------+
The dotted-decimal address 198.51.100.223
+------------------------------------------------------------------------+
¦ 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 ¦
+------------------------------------------------------------------------+
The binary address 11000110001100110110010011011111
+------------------------------------------------------------------------+
¦ 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*.
Binary address: 11000110001100110110010011011111
+-----------------------------------------------------+
¦ 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 2
8
= 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.
For example, the IPv4 address 198.51.100.223
and network mask 255.255.248.0
+-------------------------------------------------------------+
¦ 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.
This method is used to determine if two addresses are on the same or different networks*. For example, if you want to determine if your example address is on the same network with a target address, 198.51.102.57
, find the example network address (as above). Next, find the target network address using the same network mask (addresses on the same network use the same network mask, and you may not have the target mask, only the target address).
+-------------------------------------------------------------+
¦ 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.
Now, test if the example address is on the same network as the 74.125.69.100
Google address.
+-------------------------------------------------------------+
¦ 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.
Network mask to host 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 ¦
+-----------------------------------------------------------------+
Host mask to network mask
+--------------------------------------------------------------+
¦ OCTET # ¦ 1 ¦ 2 ¦ 3 ¦ 4 ¦
¦------------------+----------+----------+----------+----------¦
¦ BINARY HOST MASK ¦ 00000000 ¦ 00000000 ¦ 00000111 ¦ 11111111 ¦
¦ BITWISE NOT ¦ 11111111 ¦ 11111111 ¦ 11111000 ¦ 00000000 ¦
¦------------------+----------+----------+----------+----------¦
¦ NETWORK MASK ¦ 255 ¦ 255 ¦ 248 ¦ 0 ¦
+--------------------------------------------------------------+
It is possible to use subtraction to create a host mask from the network mask, or the network mask from the host mask by subtracting the starting mask from the longest mask (/32
all-ones mask 255.255.255.255
).
Binary network mask to host mask
+-----------------------------------------------------------------+
¦ 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 ¦
+-----------------------------------------------------------------+
Binary host mask to network mask
+--------------------------------------------------------------+
¦ 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 ¦
+--------------------------------------------------------------+
Decimal network mask to host mask
+------------------------------------------+
¦ OCTET # ¦ 1 ¦ 2 ¦ 3 ¦ 4 ¦
¦------------------+-----+-----+-----+-----¦
¦ DECIMAL /32 MASK ¦ 255 ¦ 255 ¦ 255 ¦ 255 ¦
¦ NETWORK MASK ¦ 255 ¦ 255 ¦ 248 ¦ 0 ¦
¦------------------+-----+-----+-----+-----¦
¦ HOST MASK ¦ 0 ¦ 0 ¦ 7 ¦ 255 ¦
+------------------------------------------+
Decimal Host mask to Network mask
+------------------------------------------+
¦ OCTET # ¦ 1 ¦ 2 ¦ 3 ¦ 4 ¦
¦------------------+-----+-----+-----+-----¦
¦ DECIMAL /32 MASK ¦ 255 ¦ 255 ¦ 255 ¦ 255 ¦
¦ HOST MASK ¦ 0 ¦ 0 ¦ 7 ¦ 255 ¦
¦------------------+-----+-----+-----+-----¦
¦ NETWORK MASK ¦ 255 ¦ 255 ¦ 248 ¦ 0 ¦
+------------------------------------------+
A network broadcast address is the network address with all the host bits set to 1
. There are several ways to calculate the network broadcast address.
For example, address 198.51.100.223
and network mask 255.255.248.0
.
You can perform a bitwise OR
with the address or network address with the host mask.
+---------------------------------------------------------------+
¦ 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 (2
11
= 2048
).
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 (2
11
- 2 = 2046
).
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 ¦
+------------------------------------------------------------------+
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 ¦
+----------------------------------------------------------------------+
/31
(255.255.255.254
) NetworksOriginally, /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 (2
2
– 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.
/32
(255.255.255.255
) NetworksA /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.
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 ¦
¦ NETWORK BROADCAST ADDRESS ¦ 198.51.103.255 ¦
¦ 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:
1
, an odd number)1
, an even 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...
Continued from Part 1...
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 = 2
4
). 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: Log
2
(X subnets) = Y borrowed bits
, rounded up to the next integer value.
Log
2
(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 0
s* 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)
= 2
2
= 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.
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:
500 hosts
100 hosts
200 hosts
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.
1000 hosts
500 hosts
200 hosts
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
.
1024 total hosts = 2
10
= 10 host bits
512 total hosts = 2
9
= 9 host bits
256 total hosts = 2
8
= 8 host bits
128 total hosts = 2
7
= 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: Log
2
(X hosts) = Y host bits
, rounded up to the next integer value.
Log
2
(1000 hosts) = 9.96578428466209, rounded up = 10 host bits
Log
2
( 500 hosts) = 8.96578428466209, rounded up = 9 host bits
Log
2
( 200 hosts) = 7.64385618977472, rounded up = 8 host bits
Log
2
( 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 ¦
+-------------------------------------------------------------------------------+
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 ¦
¦ NETWORK BROADCAST ADDRESS ¦ 198.51.101.191 ¦
¦ 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.
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.
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.
There are three address ranges defined a IPv4 Private Addressing:
10.0.0.0/8
172.16.0.0/12
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 D addresses are used for multicast, where each address is used individually to represent a destination group address of hosts that subscribe to a multicast address, but multicast addresses cannot be used for host or source addresses. That means that Class D addresses do not normally have the concept of a network mask. Although a mask or mask length is often used to specify a range of multicast addresses, it is not the same as a network address.
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.
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:
As a shortcut, you can also use this formula. It works on subnets of any size:
(In an attempt to keep all the netmask answers in one place, after the other excellent answers, I've added this one about a visual method.)
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.
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:
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......