Subnet Calculator

Netmask, CIDR prefix, network and broadcast address, and usable host range — worked out in binary, step by step
Find the network address, broadcast address and usable host range for 192.168.10.130/26
Convert the subnet mask 255.255.254.0 to CIDR notation and count the usable hosts
What prefix length is needed for a subnet with 100 hosts?
Are 172.16.20.10 and 172.16.31.200 on the same /20 subnet?

An IP Address Is 32 Bits

An IPv4 address is a 32-bit unsigned integer, written as four 8-bit octets in decimal. Every subnetting question is a question about those 32 bits, so the first move is always to convert to binary using the octet place values 128,64,32,16,8,4,2,1128, 64, 32, 16, 8, 4, 2, 1:

DecimalBinary
19211000000
16810101000
1000001010
13010000010

So 192.168.10.130 is 11000000.10101000.00001010.10000010.

A prefix length /n/n means the mask is nn consecutive 1 bits followed by 32n32-n zero bits. The 1 bits mark the network portion, the 0 bits the host portion:

  • /2411111111.11111111.11111111.00000000 = 255.255.255.0
  • /2611111111.11111111.11111111.11000000 = 255.255.255.192

Going the other way, convert each mask octet to binary and count the ones: 255255 is eight ones and 254254 is 11111110, seven ones, so 255.255.254.0 is /23.

The condition people forget: a valid mask is contiguous ones followed by zeros. 255.255.255.192 is legal because 192=192 = 11000000. 255.255.255.160 is not, because 160=160 = 10100000 — its ones are separated by a zero.

Network and Broadcast by Bitwise AND

Network address

AND the address with the mask, bit by bit (11=11 \wedge 1 = 1, everything else 00). Octets masked by 255 pass through unchanged, so only the boundary octet does real work. For 192.168.10.130/26:

130  = 1 0 0 0 0 0 1 0
192  = 1 1 0 0 0 0 0 0   (mask)
AND  = 1 0 0 0 0 0 0 0  = 128

Network = 192.168.10.128.

Broadcast address

Keep the network bits, set every host bit to 1: 1000000010111111 =128+63=191= 128 + 63 = 191, so the broadcast is 192.168.10.191.

Host count

With h=32nh = 32 - n host bits:

addresses=2h,usable hosts=2h2\text{addresses} = 2^{h}, \qquad \text{usable hosts} = 2^{h} - 2

The 2-2 removes the network address (all host bits 0) and the broadcast (all host bits 1). A /26 has h=6h = 6: 26=642^6 = 64 addresses, 6262 usable.

Block size shortcut

256192=64256 - 192 = 64, so /26 subnets start every 64 addresses: .0, .64, .128, .192. To size a subnet instead, find the smallest hh with 2h2H2^{h} - 2 \ge H and take n=32hn = 32 - h.

Common Mistakes to Avoid

  • Assuming the given address is the network address. 192.168.10.130/26 is a host inside 192.168.10.128/26. Always AND first.
  • Subtracting 2 where it does not apply. A /31 point-to-point link (RFC 3021) gives 2 usable addresses, and a /32 is a single host route.
  • Off-by-one on the broadcast. Broadcast == network ++ block size 1-1, not network ++ block size.
  • Reading the prefix backwards. A larger nn means a smaller network: /26 holds a quarter of what /24 holds.
  • Non-contiguous masks. Only masks of the form ones-then-zeros are valid, which is why the mask octet is always one of 0, 128, 192, 224, 240, 248, 252, 254, 255.
  • Carrying IPv4 habits into IPv6. IPv6 has no broadcast address and no 2-2 rule; a /64 simply holds 2642^{64} addresses.

示例题目

Step 1: Mask for /26: 26 ones then 6 zeros → 255.255.255.192
Step 2: Only the fourth octet is split. Write it in binary: 130=130 = 10000010, mask 192=192 = 11000000
Step 3: Bitwise AND: 10000010 \wedge 11000000 == 10000000 =128= 128, so the network is 192.168.10.128
Step 4: Set the 6 host bits to 1: 10111111 =128+32+16+8+4+2+1=191= 128 + 32 + 16 + 8 + 4 + 2 + 1 = 191, so the broadcast is 192.168.10.191
Step 5: Host bits h=3226=6h = 32 - 26 = 6, giving 26=642^6 = 64 addresses and 642=6264 - 2 = 62 usable
Answer: Network 192.168.10.128, broadcast 192.168.10.191, usable range 192.168.10.129192.168.10.190, 62 usable hosts

Step 1: Convert each octet: 255=255 = 11111111, 255=255 = 11111111, 254=254 = 11111110, 0=0 = 00000000
Step 2: Count the leading ones: 8+8+7+0=238 + 8 + 7 + 0 = 23, so the mask is /23
Step 3: Host bits h=3223=9h = 32 - 23 = 9
Step 4: Total addresses =29=512= 2^9 = 512; usable =5122=510= 512 - 2 = 510
Step 5: Check with the block size shortcut: 256254=2256 - 254 = 2, so each /23 spans two full third-octet blocks — 2×256=5122 \times 256 = 512 addresses
Answer: 255.255.254.0 = /23, 512 addresses, 510 usable hosts

Step 1: Need 2h21002^{h} - 2 \ge 100. With h=6h = 6: 262=622^6 - 2 = 62, too small. With h=7h = 7: 272=1261002^7 - 2 = 126 \ge 100
Step 2: So h=7h = 7 and n=327=25n = 32 - 7 = 25; the mask is 11111111.11111111.11111111.10000000 == 255.255.255.128
Step 3: Block size =256128=128= 256 - 128 = 128, so the /25 subnets of 10.0.5.0/24 start at .0 and .128
Step 4: Second subnet: network 10.0.5.128, host bits all 1 → 11111111 =255= 255, broadcast 10.0.5.255
Step 5: Usable range 10.0.5.12910.0.5.254, which is 126126 addresses — 26 spare
Answer: /25 (255.255.255.128), 126 usable hosts; the second subnet is 10.0.5.128/25, range 10.0.5.12910.0.5.254

常见问题

Convert both to binary and take the bitwise AND, one bit at a time. Octets where the mask is 255 pass through unchanged, so in practice only the octet that the prefix splits needs working out. For 192.168.10.130/26 the fourth octet is 10000010 AND 11000000 = 10000000 = 128, giving the network 192.168.10.128.

A /26 leaves 32 − 26 = 6 host bits, so 2^6 = 64 addresses. Two are reserved — the all-zeros network address and the all-ones broadcast address — leaving 62 usable host addresses.

Write each mask octet in binary and count the leading 1 bits. 255.255.255.0 is 8 + 8 + 8 = /24, and 255.255.254.0 is 8 + 8 + 7 = /23. Only masks made of contiguous ones followed by zeros are valid, so each octet must be one of 0, 128, 192, 224, 240, 248, 252, 254 or 255.

The −2 exists because the all-zeros and all-ones addresses are reserved for the network and broadcast. A /32 is a single-address host route, and RFC 3021 allows a /31 to be used on point-to-point links where no broadcast is needed, so both of its addresses are usable.

免费试用 AI-Math

任何数学问题都能获得分步解答。拍照上传或输入问题即可。

开始解题