ScienceComputer Science

IP Subnet
calculator.

Type an IPv4 address with a prefix or a mask to get its network, hosts and broadcast.

How it's worked out

An IPv4 address is 32 bits, written as four octets of 8 bits each. The prefix length, the number after the slash, says how many of those bits name the network. The rest name a device on it. RFC 4632 sets this notation and requires the mask to be one run of ones followed by zeros.

addresses\displaystyle \text{addresses}=232−p\displaystyle {}= 2^{\,32 - p}
usable hosts\displaystyle \text{usable hosts}=232−p−2\displaystyle {}= 2^{\,32 - p} - 2

Here pp is the prefix length. To find the network, line the address up with the mask and keep each bit where the mask has a 1. For 192.168.1.130/26 only the last octet is split. 130 is 10000010 in binary and the mask octet 192 is 11000000, so the network octet is 10000000, which is 128.

The AND for 192.168.1.130/26, one octet at a time
OctetAddressMaskNetwork
1192255192
2168255168
312551
4130192128

Set every host bit to 1 instead and you get the broadcast address, 192.168.1.191. RFC 1122 keeps the all-zeros and all-ones host numbers for the network and for broadcast, so a device never gets them. That is where the minus 2 comes from. The one exception is a /31 on a link between two routers: RFC 3021 lets both of its 2 addresses be hosts.

Worked examples

Priya sets up a VLAN for the design team

Priya was handed 192.168.1.130/26 for a new team switch. A /26 leaves 6 host bits, so the address space is cut into blocks of 64. 130 falls in the block that starts at 192.168.1.128.

Devices can use 192.168.1.129 to 192.168.1.190, which is 62 addresses. The broadcast address is 192.168.1.191 and the mask to type into the router is 255.255.255.192. A printer set to 192.168.1.20 would not be on this VLAN: it belongs to 192.168.1.0/26.

Open this example in the tool

Marco reads a mask from an old router config

The config lists 172.16.5.4 with mask 255.255.240.0. In binary 240 is 11110000, so the mask has 8 + 8 + 4 ones, a /20. The third octet of the address, 5, is 00000101. Its top four bits are all 0, so the network is 172.16.0.0.

That one network runs to 172.16.15.255 and holds 4,094 usable addresses. The address is private, inside the 172.16.0.0/12 block from RFC 1918.

Open this example in the tool

Dana gives each of four floors its own subnet

Dana has 192.168.10.0/24 and four floors. Splitting into 4 borrows 2 host bits, so each piece is a /26 with 62 usable hosts.

The four networks start at 192.168.10.0, 192.168.10.64, 192.168.10.128, 192.168.10.192. Each block starts on a multiple of 64, its own size. If one floor later needs 100 devices, a /25 there and smaller blocks elsewhere would fit better; the list of host counts in the tool plans that.

Open this example in the tool

Reading your result

The network line is the block your address belongs to, written as its first address and the prefix. The host range is what you can give to devices, including the router that acts as the gateway. Usable hosts is the block size minus 2, so a /24 gives 254.

The masks say the same thing in other forms. The subnet mask, such as 255.255.255.192, goes into device settings. The wildcard mask, 0.0.0.63 here, flips every bit. Cisco access lists and OSPF network lines use it, where a 1 means that bit may be anything.

The address type comes from the IANA special-purpose registry. Private blocks (10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16) never route on the internet, so a home or office reaches the outside through NAT. The documentation blocks, such as 203.0.113.0/24, exist only for examples. 100.64.0.0/10 is the shared space internet providers use for carrier-grade NAT.

Common prefixes
PrefixSubnet maskUsable hosts
/16255.255.0.065,534
/20255.255.240.04,094
/22255.255.252.01,022
/24255.255.255.0254
/25255.255.255.128126
/26255.255.255.19262
/27255.255.255.22430
/28255.255.255.24014
/29255.255.255.2486
/30255.255.255.2522
/31255.255.255.2542
/32255.255.255.2551

Cloud networks take a few more. In an AWS VPC the first four and the last address of each subnet are reserved, so a /24 there leaves 251 addresses. AWS subnets also must be between /16 and /28.

Common mistakes

  • Forgetting the 2 reserved addresses. 62 devices fit a /26, but 63 need a /25. Count the gateway as a device too.
  • Masks that do not match. A laptop at 192.168.1.20/24 treats 192.168.1.130 as local and sends to it directly. A server at 192.168.1.130/26 sees 192.168.1.20 as outside its block and sends replies to its gateway. Traffic then works in one direction only, or not at all.
  • Blocks that do not start on a boundary. A /26 has to start on a multiple of 64 in its last octet: .0, .64, .128 or .192. 192.168.1.100/26 is a host inside 192.168.1.64/26, not a network of its own.
  • Sizing subnets smallest first. Hand out the largest block first, then the next. Starting with small ones leaves gaps that a large block cannot use.
  • Using a /31 on an ordinary LAN. The RFC 3021 rule is for point-to-point links. On an Ethernet segment with a switch, many devices still expect a network and a broadcast address.

Questions people ask

A /26 has 6 host bits, so it holds 64 addresses. Take away the network and broadcast addresses and 62 are left for devices. Each additional prefix bit halves that: a /27 gives 30.

Sources