Network Mask Calculation

How to convert between slash notation, decimal mask, and binary mask. Learn the binary structure behind every subnet mask, with practical examples for network engineers and students.

Network cabling for subnet mask binary calculation
What Is a Network Mask? Slash to Decimal Binary Breakdown AND with IP Common Masks Table Wildcard Masks FAQ

What Is a Network Mask?

A network mask (also called a subnet mask or netmask) is a 32-bit binary value that tells a router or host how to split an IP address into its network portion and its host portion. Every bit that is set to 1 in the mask means "this bit belongs to the network address," and every bit set to 0 means "this bit belongs to the host address."

In my day-to-day work configuring routers and diagnosing connectivity issues, I constantly convert between the three representations of the same underlying binary pattern: slash notation (also called CIDR prefix length, like /24), dotted-decimal mask (like 255.255.255.0), and binary mask (like 11111111.11111111.11111111.00000000). These three forms are equivalent — the slash notation is just a count of consecutive 1 bits from the left; the decimal form is those 32 bits grouped into four 8-bit octets. Once you understand the binary, all three make perfect sense.

When I was partitioning a /24 into four /26 subnets for a multi-tier application, I did the mask calculations by hand to double-check the infrastructure-as-code output. Each /26 adds 2 bits to the mask (192 in decimal), and I wanted to be sure no subnets overlapped.

The mask is applied using a bitwise AND operation between the IP address and the mask. If you already understand how bitwise AND works (and if not, our bitwise operations guide covers it in detail), you already know how subnetting works at the binary level. It is the same operation: 1 AND 1 = 1 keeps the bit, 1 AND 0 = 0 clears it.

Key Insight

A subnet mask is always a contiguous sequence of 1 bits followed by a contiguous sequence of 0 bits. There are no gaps. If you see 255.255.255.0, that is 24 ones followed by 8 zeros. Masks like 255.255.0.255 are not valid because the ones are not contiguous.

Converting Slash Notation to Decimal Mask

Slash notation (CIDR notation) is the simplest: the number after the slash tells you how many leading 1 bits the mask has. The remaining (32 - N) bits are zeros. To convert, I write N ones, pad with zeros to reach 32 bits, split into four octets, and convert each octet to decimal.

Here is the conversion formula in a nutshell:

Slash to Decimal Conversion Formula
Convert /N to decimal:
1. Write N ones followed by (32 - N) zeros
2. Split into 4 groups of 8 bits (octets)
3. Convert each octet to decimal

Example: /24
Binary: 11111111 11111111 11111111 00000000
Decimal: 255.255.255.0

Let me walk through a few more so the pattern becomes clear. For each slash value, the octets where all bits are 1 become 255, octets with a mix of ones and zeros require a quick binary-to-decimal conversion, and octets with all zeros become 0.

/20 Mask Conversion
/20 = 20 ones + 12 zeros
Binary: 11111111.11111111.1111 0000.00000000
Octet 1: 11111111 = 255
Octet 2: 11111111 = 255
Octet 3: 11110000 = 240 (128+64+32+16)
Octet 4: 00000000 = 0
Decimal: 255.255.240.0
/20 = 255.255.240.0 — allows 2^12 - 2 = 4094 host addresses
/28 Mask Conversion
/28 = 28 ones + 4 zeros
Binary: 11111111.11111111.11111111.1111 0000
Octet 1: 11111111 = 255
Octet 2: 11111111 = 255
Octet 3: 11111111 = 255
Octet 4: 11110000 = 240
Decimal: 255.255.255.240
/28 = 255.255.255.240 — allows 2^4 - 2 = 14 host addresses per subnet

Binary Breakdown: Reading the Mask as Bits

Every time I look at a decimal mask, I mentally convert the non-255 or non-0 octets to binary. This tells me exactly where the network-host boundary falls within that octet. The boundary always falls on a bit boundary — never between bits — but it can fall in the middle of an octet.

For example, 255.255.255.192 is a /26 mask. The last octet, 192, has binary 11000000, meaning the top 2 bits of the last octet are part of the network prefix and the bottom 6 bits are the host portion. This tells me each subnet has 2^6 - 2 = 62 usable addresses, and the subnets increment by 64 in the last octet (0, 64, 128, 192).

Binary breakdown of common masks:

255.255.255.192    /26
  Binary:  11111111.11111111.11111111.11000000
  Network bits: 26   |   Host bits: 6
  Subnets: 2^(26-24) = 4   |   Hosts per subnet: 62

255.255.255.224    /27
  Binary:  11111111.11111111.11111111.11100000
  Network bits: 27   |   Host bits: 5
  Subnets: 2^(27-24) = 8   |   Hosts per subnet: 30

255.255.255.248    /29
  Binary:  11111111.11111111.11111111.11111000
  Network bits: 29   |   Host bits: 3
  Subnets: 2^(29-24) = 32  |   Hosts per subnet: 6

The pattern is straightforward: the number of host bits tells you the block size (2^host_bits), and the smallest nonzero value in the interesting octet (the octet where the boundary falls) tells you the subnet increment. For /26, the interesting octet value is 192, and the increment is 256 - 192 = 64.

The Bitwise AND: How Masks Work with IP Addresses

The actual computation a router performs is a bitwise AND between the IP address and the mask. This produces the network address — the base address of the subnet, with all host bits set to zero. I find it helpful to write both the IP and the mask in binary, line them up, and do the AND bit by bit.

Here is how it works for the IP 192.168.5.130 with a /26 mask (255.255.255.192):

IP:      192.168.5.130
Binary:  11000000.10101000.00000101.10000010

Mask:    255.255.255.192
Binary:  11111111.11111111.11111111.11000000

AND:     ---------------------------------------
         11000000.10101000.00000101.10000000

Result:  192.168.5.128  (network address)
Block:   192.168.5.128 - 192.168.5.191
Broadcast: 192.168.5.191  (host bits all 1s)
Usable:  192.168.5.129 - 192.168.5.190 (62 addresses)

Notice what happened: the mask retained the first 26 bits of the IP exactly as they were (because the mask has 1s there) and zeroed out the last 6 bits (because the mask has 0s there). The result 192.168.5.128 is the network address for that subnet. The broadcast address is obtained by setting all host bits to 1: 192.168.5.191. The usable range sits between them.

This same AND operation happens at every hop along a packet's path. When a router receives a packet destined for 192.168.5.130, it ANDs the destination with each route's subnet mask until it finds a match. The most specific match wins — this is called longest prefix match, and it is the fundamental forwarding algorithm of the internet.

Quick AND Calculation

You do not actually need to convert the full IP to binary every time. For the octet where the mask is not 255 or 0, you can use the block size method: the network portion of that octet is the largest multiple of the block size that is less than or equal to the IP octet. For /26, the block size is 64, and 130 falls between 128 and 191, so the network octet is 128.

Common Network Masks Reference Table

Here is a quick reference I keep handy for the most frequently used masks. The "bits borrowed" column assumes a default Class C (/24) network, which is the most common starting point for subnetting in practice.

Slash   Decimal Mask       Binary Mask                        Hosts       Block
/24     255.255.255.0      11111111.11111111.11111111.00000000   254      256
/25     255.255.255.128    11111111.11111111.11111111.10000000   126      128
/26     255.255.255.192    11111111.11111111.11111111.11000000    62       64
/27     255.255.255.224    11111111.11111111.11111111.11100000    30       32
/28     255.255.255.240    11111111.11111111.11111111.11110000    14       16
/29     255.255.255.248    11111111.11111111.11111111.11111000     6        8
/30     255.255.255.252    11111111.11111111.11111111.11111100     2        4
/16     255.255.0.0        11111111.11111111.00000000.00000000  65534    65536
/8      255.0.0.0          11111111.00000000.00000000.00000000  16.7M   16.7M

The /30 mask is special: it provides exactly 2 usable host addresses per subnet, making it the standard for point-to-point links between routers. You will see /30 and /31 (which allows 2 addresses with no broadcast) on WAN interfaces between provider and customer equipment.

Wildcard Masks: The Inverse

A wildcard mask is simply the bitwise NOT of the subnet mask. Where the subnet mask has a 1, the wildcard mask has a 0, and vice versa. Wildcard masks are used in Cisco IOS access control lists (ACLs) and OSPF network statements to specify which bits of an address must match exactly and which can vary.

For a /24 network with mask 255.255.255.0, the wildcard mask is 0.0.0.255. This means "match the first three octets exactly; allow any value in the last octet."

Computing Wildcard Masks
Subnet: 10.0.0.0 /16
Mask: 255.255.0.0 = 11111111.11111111.00000000.00000000
Wildcard: 0.0.255.255 = 00000000.00000000.11111111.11111111

Subnet: 172.16.0.0 /20
Mask: 255.255.240.0 = 11111111.11111111.11110000.00000000
Wildcard: 0.0.15.255 = 00000000.00000000.00001111.11111111
Wildcard = 255.255.255.255 - subnet_mask (octet by octet)

The quick way to compute a wildcard mask: subtract each octet of the subnet mask from 255. For 255.255.240.0, that gives 0.0.15.255. No binary conversion needed — just make sure the result makes sense (it should have a contiguous block of zeros corresponding to the network bits).

Calculate Network Masks on the Fly

Use our programmer calculator to convert between slash notation, decimal masks, and binary masks. Enter any mask in any format and see the equivalent values across all three representations instantly.

Frequently Asked Questions About Network Mask Calculation

What is a network mask and why is it written in binary?

A network mask (or subnet mask) is a 32-bit value that separates the network portion of an IP address from the host portion. It is inherently binary — each bit in the mask is either 1 (meaning the corresponding IP bit belongs to the network prefix) or 0 (meaning it belongs to the host). The standard dotted-decimal notation like 255.255.255.0 is just a human-friendly way to write the binary sequence 11111111.11111111.11111111.00000000. Understanding the binary form is essential because it shows exactly where the boundary between network and host falls.

How do I convert slash notation to a decimal mask?

To convert slash notation (CIDR prefix length) to a dotted-decimal mask, write N consecutive 1 bits followed by (32 - N) zero bits, group them into four octets, and convert each octet to decimal. For example, /24 means 24 ones followed by 8 zeros: 11111111.11111111.11111111.00000000, which is 255.255.255.0. For /20, you get 11111111.11111111.11110000.00000000 = 255.255.240.0. You can think of it as filling ones from the leftmost bit until you reach the prefix length.

What is the binary pattern behind 255.255.255.0?

255.255.255.0 in binary is 11111111.11111111.11111111.00000000. The first three octets are all ones, meaning those 24 bits are the network portion. The last octet is all zeros, meaning those 8 bits are available for host addresses. This gives 2^8 - 2 = 254 usable host addresses (the all-zeros address is the network address, and the all-ones address is the broadcast address). This is the most common mask for small office and home networks.

How does a router use the mask with bitwise AND?

When a router receives a packet, it performs a bitwise AND between the destination IP address and the subnet mask to extract the network address. For example, if the destination is 192.168.1.55 and the mask is 255.255.255.0 (/24), the router computes 192.168.1.55 AND 255.255.255.0 = 192.168.1.0. The router then looks up 192.168.1.0 in its routing table to determine the correct next hop. This bitwise AND operation is executed in hardware at line rate, which is why it is so fast.

What is the difference between a network mask and a wildcard mask?

A wildcard mask is the bitwise inverse of a network mask. Where the network mask has a 1 (network portion), the wildcard mask has a 0, and where the network mask has a 0 (host portion), the wildcard mask has a 1. For a /24 network with mask 255.255.255.0 (11111111.11111111.11111111.00000000), the wildcard mask is 0.0.0.255 (00000000.00000000.00000000.11111111). Wildcard masks are used in router access control lists (ACLs) and OSPF network statements. To compute a wildcard mask, simply take the bitwise NOT of the subnet mask.

Related Tools

Bitwise Calculator →

AND, OR, XOR, NOT, shifts

Bitwise Guide →

Complete operations reference

Hex to ASCII →

Decode hex strings

SHA256 Generator →

Compute hashes