ip
ipku.in

How to Calculate IPv4 Addresses

Subnetting IPv4 from scratch: turn a prefix into a subnet mask, find the network and broadcast, then count the hosts. Every step uses the same example.

Updated · Jetorbit

01 / summary

IPv4 subnet formulas at a glance

Quick example: 192.168.10.77/26 has subnet mask 255.255.255.192, network 192.168.10.64, broadcast 192.168.10.127, and 62 usable hosts. Each step is explained below.

02 / basics

What an IPv4 address is made of

An IPv4 address is 32 bits split into four octets. Each octet holds 8 bits, so it ranges from 0 to 255. Octets are written in decimal and separated by dots.

decimal192.168.10.77
binary11000000.10101000.00001010.01001101

From left to right, the bits in an octet are worth 128, 64, 32, 16, 8, 4, 2, and 1. For example 77 = 64 + 8 + 4 + 1, so it is written 01001101.

Every IPv4 address has two parts. The left part is the network the device sits on. The right part is the host, the device within that network. The prefix or subnet mask sets where one ends and the other begins.

03 / cidr

CIDR prefixes and subnet masks

The prefix comes after a slash, such as /24 or /26. It says how many bits from the left belong to the network. This notation is called CIDR (Classless Inter-Domain Routing).

A subnet mask is the prefix written as an address. Network bits are 1 and host bits are 0. For /26, that is 26 ones followed by 6 zeros.

prefix/26
binary11111111.11111111.11111111.11000000
subnet mask255.255.255.192
wildcard0.0.0.63

A wildcard mask is the inverse of the subnet mask: 255 minus each octet. Router ACLs and some firewalls use it.

Subnet mask octet values

A subnet mask octet can only take one of the values below, depending on how many 1 bits it has. The block size is the step between subnets in that octet.

1 bitsoctet valueblock size
1128128
219264
322432
424016
52488
62524
72542
82551
04 / calculate

How to calculate an IPv4 subnet, step by step

Example: a device uses 192.168.10.77/26. Here is how to find its network, broadcast, and host range.

  1. Turn the prefix into a subnet mask. /26 is three full octets (24 bits) plus 2 bits of the fourth, so the mask is 255.255.255.192.
  2. Count the addresses per subnet: 2⁶ = 64, because there are 6 host bits.
  3. Work out the block size in the partial octet: 256 − 192 = 64. Subnets in the fourth octet start at multiples of 64: 0, 64, 128, 192.
  4. Find the block the address falls in. 77 is between 64 and 127, so the network address is 192.168.10.64.
  5. The broadcast is the last address in that block: 192.168.10.127.
  6. Usable hosts sit in between: 192.168.10.65 to 192.168.10.126, which is 64 − 2 = 62 hosts.
address192.168.10.77/26
subnet mask255.255.255.192
network192.168.10.64
first host192.168.10.65
last host192.168.10.126
broadcast192.168.10.127
usable hosts62

The binary way: AND

You can also get the network address by ANDing the IP with the subnet mask, bit by bit. A result bit is 1 only when both bits are 1. For the broadcast, set every host bit to 1.

ip11000000.10101000.00001010.01001101
mask11111111.11111111.11111111.11000000
AND11000000.10101000.00001010.01000000
network192.168.10.64
05 / subnetting

Subnetting: splitting one network into several subnets

Subnetting borrows host bits and turns them into network bits. Each borrowed bit doubles the number of subnets and halves the addresses in each one.

Example: split 192.168.10.0/24 into 4 subnets. That takes 2 bits, since 2² = 4, so the new prefix is /26 with 62 hosts each.

subnetnetworkhost rangebroadcast
1192.168.10.0/26192.168.10.1 – 192.168.10.62192.168.10.63
2192.168.10.64/26192.168.10.65 – 192.168.10.126192.168.10.127
3192.168.10.128/26192.168.10.129 – 192.168.10.190192.168.10.191
4192.168.10.192/26192.168.10.193 – 192.168.10.254192.168.10.255

If you start from a host count, pick the smallest subnet that still fits. Need 50 hosts? A /26 gives 2⁶ − 2 = 62 and fits, while a /27 gives only 2⁵ − 2 = 30.

06 / reference

IPv4 subnet mask and prefix table

prefixsubnet maskaddressesusable hosts
/8255.0.0.016,777,21616,777,214
/16255.255.0.065,53665,534
/20255.255.240.04,0964,094
/21255.255.248.02,0482,046
/22255.255.252.01,0241,022
/23255.255.254.0512510
/24255.255.255.0256254
/25255.255.255.128128126
/26255.255.255.1926462
/27255.255.255.2243230
/28255.255.255.2401614
/29255.255.255.24886
/30255.255.255.25242
/31255.255.255.25422
/32255.255.255.25511

/31 is used for point-to-point links between routers, so both addresses are usable (RFC 3021). /32 is a single address, such as one server.

07 / ranges

Special IPv4 address ranges

rangeused for
10.0.0.0/8private (local networks)
172.16.0.0/12private (local networks)
192.168.0.0/16private, common on home routers
100.64.0.0/10CGNAT: an ISP sharing one public IP among many customers
127.0.0.0/8loopback, the device itself
169.254.0.0/16link-local: shows up when a device gets no address from DHCP
192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24documentation and examples

What about class A, B, and C?

Classes were replaced by CIDR in 1993, but they are still taught. The class comes from the first octet. Class A (1–126) has a default /8 mask, class B (128–191) has /16, and class C (192–223) has /24. With CIDR, a prefix can be any length regardless of class.

08 / jetorbit

Further reading on Jetorbit

These articles are in Indonesian.