A subnet mask is a 32-bit value paired with an IPv4 address. It marks the bits that identify the network and the bits that identify a particular device, or host, on that network. That lets TCP/IP determine whether a destination is on the local subnet or a remote network. 255.255.255.0, for example, means the first 24 bits identify the network and the remaining eight identify the host.
Subnet mask explained in bits
An IPv4 address contains 32 binary digits, usually written for humans as four decimal numbers separated by periods. Each eight-bit group is an octet. A subnet mask is another 32-bit number written in the same dotted-decimal format.
In a valid mask, contiguous 1 bits begin at the left and identify the network portion. The remaining 0 bits identify the host portion. The boundary can fall inside an octet, which is why masks such as 255.255.255.192 exist.
- 255 in binary is 11111111, eight network bits.
- 0 in binary is 00000000, eight host bits.
- 192 in binary is 11000000, two network bits followed by six host bits.
Worked example: 192.168.123.132 with 255.255.255.0
Line up the address and mask in binary:
- IP address: 11000000.10101000.01111011.10000100
- Subnet mask: 11111111.11111111.11111111.00000000
The mask has 24 one bits, so its CIDR, or Classless Inter-Domain Routing, form is /24. CIDR writes the number of network bits after a slash. Thus 255.255.255.0 = /24.
Keep the IP-address bits wherever the mask has a 1, then replace the host-bit positions with zeroes. That produces the network address:
- Network address: 192.168.123.0
- Host portion: 0.0.0.132
Formally, this is a bitwise AND operation: a 1 in the mask preserves the corresponding address bit, while a 0 clears it. A device can compare its own network address with a destination's. A matching result indicates the local subnet; a different result identifies a remote network. Routers use route-table information to forward packets between networks.
Why /24 and 255.255.255.0 say the same thing
A /24 has 24 network bits and eight host bits. A /26 has 26 network bits and six host bits, which corresponds to 255.255.255.192.
For example, 192.0.2.5/26 belongs to network 192.0.2.0. In that example, the broadcast address is 192.0.2.63 and the usable host range is 192.0.2.1 through 192.0.2.62. Subnetting divides an IP network into smaller subnets.
A subnet mask is not a subnet
The terms get casually swapped, which makes configuration guides harder than they need to be. The mask is the bit boundary. The subnet is the specific network segment identified using that boundary.
- Mask: 255.255.255.0, also written /24.
- Subnet: 192.168.123.0/24.
Frequently asked questions
How do I convert 255.255.255.0 to /24?
Convert the mask to binary and count its 1 bits. Each 255 represents eight 1 bits, so 255.255.255.0 has 24 leading 1 bits and is written /24.
How can I tell whether two IPv4 addresses are on the same subnet?
Apply the same subnet mask to each address using a bitwise AND operation. If both operations produce the same network address, they are on the same subnet. If the results differ, they identify different networks.
What is the difference between a subnet mask and a subnet?
A subnet mask is the 32-bit pattern that separates network bits from host bits. A subnet is the specific network segment identified by an address and that mask, such as 192.168.123.0/24.
How many usable host addresses does a /26 subnet have?
A /26 has six host bits. In the supplied 192.0.2.0/26 example, there are 62 usable host addresses, from 192.0.2.1 through 192.0.2.62; .0 is the network address and .63 is the broadcast address.
Sources
- Understand TCP/IP addressing and subnetting basics — learn.microsoft.com
- What is a Subnet Mask? Examples, Uses and Benefits — www.auvik.com
- Subnet Mask - Explained — www.youtube.com