Networking

CIDR, IPv4 subnet and IPv6: what the mask does in binary

Almost everyone memorizes the table: /24 is 255.255.255.0, /26 is 255.255.255.192. But a mask is not a table, it is binary arithmetic. Every field a subnet calculator returns (network address, broadcast, host range, total addresses) falls out of a single operation: the bitwise logical AND between the address and the mask. This is the conceptual guide: what CIDR really states, how the mask works in binary, why it was born to replace the old Class A/B/C system, and why IPv6 changes everything by dropping broadcast and NAT. If your goal is to split a production network without waste, the next step is [subnetting with VLSM in practice](guide:subnetting-cidr-mascara-vlsm-na-pratica); here we lay the foundation. Paste any prefix into the [subnet calculator](tool:calculadora-subrede) as you read, it runs entirely in the browser.

J-Kit14 min readIntermediate
  • CIDR
  • Subnet
  • IPv4
  • IPv6
  • Binary

Key takeaways

  • The /n prefix says how many bits are network; the mask is just those n bits set to 1 and the remaining 32 − n set to 0. In IPv4 the rest addresses hosts.
  • The network address is the bitwise AND between the IP and the mask; the broadcast is the OR with the wildcard. No memorizing.
  • Usable hosts = 2^(32 − prefix) − 2, minus network and broadcast, except on /31 (point-to-point) and /32 (single host).
  • IPv6 uses 128 bits, drops broadcast (it uses multicast) and NAT, and treats the /64 as the default subnet because of SLAAC.

What the CIDR prefix means

CIDR (Classless Inter-Domain Routing) describes, with a single number after the slash, how many of the address bits belong to the network. An IPv4 address is 32 bits total. A /24 reserves the first 24 bits for the network and leaves the last 8 for hosts; a /26 reserves 26 and leaves 6. The word classless is the heart of the idea: before CIDR, network size was dictated by the first octet (Classes A, B and C); with CIDR, you pick the cut point freely, bit by bit. That is what "classless" means.

From the prefix, and from it alone, the calculator derives every field you see. Each one has a precise meaning:

Network address
The first address of the block (all host bits at 0). It identifies the subnet and is not assigned to any host.
Broadcast
The last address (all host bits at 1). It sends to every host of the subnet at once, it exists only in IPv4.
Mask
The decimal form of the prefix: n bits set to 1 followed by 32 − n bits set to 0. A /24 is 255.255.255.0.
Wildcard
The inverse of the mask (each 0 becomes 1 and vice versa). It marks the host bits, handy in ACLs and for finding the broadcast.
Host range
From the first to the last usable address, between the network and the broadcast.

The mask is a binary operation, not a table

Here is what most tutorials hide behind the memorized table. To find the network address, the device performs a logical AND, bit by bit, between the address and the mask. The AND rule is simple: the result is 1 only when both bits are 1. Where the mask has a 1 (network bits), the IP bit passes through untouched; where the mask has a 0 (host bits), the result is always 0. That is why the network address has all host bits zeroed, not by convention, but because the AND erases them. See it with 192.168.10.77/26:

  IP     192.168.10.77   11000000.10101000.00001010.01001101
AND
  máscara /26           11111111.11111111.11111111.11000000   (255.255.255.192)
  mask    /26
= ------------------------------------------------------------
  rede    192.168.10.64  11000000.10101000.00001010.01000000
  network

// Só o último octeto muda: 01001101 (77) AND 11000000 = 01000000 (64).
// Only the last octet changes: 01001101 (77) AND 11000000 = 01000000 (64).
The logical AND between the IP and the mask zeroes the 6 host bits and reveals the network address: 192.168.10.64. The broadcast is the opposite, the OR with the wildcard 0.0.0.63.

The broadcast follows the same reasoning, inverted: take the logical OR between the network and the wildcard (the inverted mask). Where the wildcard has a 1, the bit becomes 1; the result fills every host bit with 1. For our example, 192.168.10.64 OR 0.0.0.63 gives 192.168.10.127. Everything else, first host, last host, count, is just walking between those two boundaries. A mask never mixes 1s and 0s out of order: it is always a run of 1s followed by a run of 0s. That is what makes the prefix (the count of 1s) enough to describe it.

How many addresses each prefix holds

The total number of addresses in an IPv4 prefix is 2 raised to the number of host bits. Since two of those addresses cannot be handed to a host (the first is the network, the last is the broadcast), usable hosts equal the total minus 2. This is the formula the calculator applies to every row:

hosts_utilizaveis = 2^(32 − prefixo) − 2
prefixo / prefix
the number after the slash (0 to 32), the count of network bits
32 − prefixo
the remaining host bits in IPv4
− 2
subtracts the network and broadcast addresses
The exponent (32 − prefix) is the number of host bits. The "− 2" removes the network and broadcast addresses, which cannot be assigned. The rule does NOT apply to /31, where RFC 3021 frees both addresses, nor to /32, which is a single host.
Common prefixes from /24 to /30, with the decimal mask, total addresses and usable hosts. Each extra prefix bit halves the block.
PrefixMaskTotal addressesUsable hosts
/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

Notice the pattern: each bit added to the prefix halves the block, 256, 128, 64, 32…, because it steals a bit from the host. It is an exponential decay, not linear. A /30, with just 2 hosts, is the smallest block that still honors the minus-two rule; it is the classic choice for point-to-point links before /31 caught on.

Why CIDR existed: the end of classes

The original IPv4 (RFC 791, from 1981) split the space into fixed classes by the value of the first octet: Class A (1–126) reserved 8 network bits and gave 16 million hosts; Class B (128–191), 16 bits and 65 thousand hosts; Class C (192–223), 24 bits and only 254 hosts. The problem was grotesque. A company with 300 machines did not fit in a /24 (Class C, 254 hosts) and got a /16 (Class B), wasting over 65 thousand addresses. There was no middle ground between 254 and 65 thousand. At the same time, every network became a line in the global routing table, which grew far too fast.

CIDR (RFC 1519, from 1993, now updated by RFC 4632, from 2006) solved both problems at once. By allowing any prefix, it delivered the /23, the /22, the /20, the in-between sizes that were missing. And by allocating blocks hierarchically per provider, it enabled route aggregation: a provider advertises one large prefix instead of thousands of small networks. RFC 6890 later consolidated the special-purpose blocks (loopback, documentation, private) into a single registry. And before that, RFC 1918 reserved the private ranges, 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16, that eased the pressure by letting addresses be reused behind NAT. Which ranges reach the internet and which stay on the LAN is the topic of the public vs private IP guide.

  1. 1981IPv4 and the classes (RFC 791)

    Addressing is born classful: the first octet decides whether the network is A, B or C, fixed sizes, no flexibility.

  2. 1993CIDR is born (RFC 1519)

    Free prefixes replace classes to contain exhaustion and slow the growth of the global routing table.

  3. 1996Private addresses (RFC 1918)

    The 10/8, 172.16/12 and 192.168/16 ranges are reserved for internal use, behind NAT, easing the demand for public IPs.

  4. 2006CIDR consolidated (RFC 4632) and IPv6

    RFC 4632 (BCP 122) becomes the current CIDR specification; in parallel, IPv6 matures with 128 bits to end scarcity for good.

  5. 3 Feb 2011IANA exhausts the IPv4 pool

    IANA allocates the last five /8 blocks to the regional registries. IPv4 is spent; the move to IPv6 stops being optional.

IPv4 and IPv6 side by side

The difference starts with size and echoes through everything else. IPv4 has 32 bits: 2^32 = 4,294,967,296 addresses, about 4.3 billion, fewer than one per person on the planet. IPv6 has 128 bits: 2^128 ≈ 3.4 × 10^38, a number so large it slips past human intuition. A single IPv6 /64 already holds 2^64 ≈ 1.8 × 10^19 addresses, more than 4 billion times the entire IPv4 internet, in one subnet. Curiously, an IPv6 address is exactly 128 bits, the same size as a UUID: 128 bits are enough to give a unique identifier to practically anything.

2^32≈ 4.3 billion IPv4 addresses
2^128≈ 3.4 × 10^38 IPv6 addresses
2^64addresses in a single /64 (≈ 1.8 × 10^19)

IPv4

  • 32 bits, about 4.3 billion addresses, scarce.
  • Dotted-decimal notation: 192.168.1.10.
  • Has a broadcast address per subnet.
  • NAT is everywhere to reuse private addresses.

IPv6

  • 128 bits, a practically inexhaustible space.
  • Hexadecimal, colon-separated notation: 2001:db8::1.
  • No broadcast; uses multicast (ff02::1 = all nodes).
  • NAT is no longer needed; every host can have a global IP.

Two IPv6 choices confuse people coming from IPv4. First: there is no broadcast. The "talk to everyone" role moved to multicast, with reserved addresses like ff02::1 (all nodes on the link) and ff02::2 (all routers). This avoids the broadcast storm and makes traffic more efficient. Second: the default subnet prefix is /64, and that is almost a law. The reason is SLAAC (stateless address autoconfiguration, RFC 4862): the host builds its own address by joining the /64 prefix advertised by the router to a 64-bit interface identifier. RFC 4291 requires a 64-bit interface identifier for most unicast addresses, which is why subnets smaller than /64 break autoconfiguration. Link-local addresses (fe80::/10) and the private ULA (fc00::/7, in practice fd00::/8, RFC 4193) round out the picture, each with its own role, as the private ranges do in IPv4.

Why /31 is valid on point-to-point links

On a link between two routers there are only two ends. A /30 worked, but it burned 4 addresses to use 2, throwing away the network and broadcast. RFC 3021 noticed that, on a link with exactly two nodes, broadcast is redundant (sending to "everyone" means sending to the other end), so it reinterprets the two addresses of a /31 as valid hosts. Result: 2 addresses, 2 hosts, zero waste. That is why the minus-two formula does not apply here.

Why nearly every IPv6 subnet is /64

It looks wasteful to give 2^64 addresses to a LAN with ten devices, but the /64 is not about quantity, it is about SLAAC. Autoconfiguration needs exactly 64 bits for the interface identifier (RFC 4862 and RFC 4291). Using a prefix longer than /64 on an access network breaks autoconfiguration and many IPv6 features. The practical rule is: host subnets are always /64; you vary the prefix only when carving larger blocks across sites (for example, handing a /48 or /56 to each location).

NAT is not a firewall (and IPv6 makes that clear)

Many people think IPv4 NAT "protects" the network by hiding internal IPs. It does not: NAT exists to save addresses, not for security. What blocks inbound connections is the firewall, with or without NAT. IPv6, with addresses to spare, drops NAT and gives every host a global IP, which alarms anyone who mistook NAT for defense. The right answer is not to reintroduce NAT in IPv6, but to configure a firewall with a default-deny inbound policy. Security is a firewall rule; address translation is a saving measure.

Two worked examples

Example 1, IPv4. Given 192.168.10.77/26, find the network, broadcast, usable range and host count. A /26 has 6 host bits (32 − 26), so the block holds 2^6 = 64 addresses and the block size is 64. The address 192.168.10.77 lands in the block starting at .64 (a /26 has blocks at .0, .64, .128, .192, and 77 sits between 64 and 127). Doing the AND we saw in the binary section, the network address is 192.168.10.64. The broadcast is the top of the block, .64 + 64 − 1 = .127. The usable range runs from the first host to the last: .65 to .126. And usable hosts are 64 − 2 = 62.

192.168.10.64network address (IP AND mask)
192.168.10.127broadcast (network OR wildcard)
.65 – .126usable host range
62usable hosts (64 − 2)

Example 2, IPv6. Compress and expand 2001:0db8:0000:0000:0008:0800:200c:417a. Compression has two rules: drop the leading zeros of each group (0db8 becomes db8, 0008 becomes 8, 0800 becomes 800) and replace the longest run of all-zero groups with a double colon (::), which may appear only once in the address. Here groups 3 and 4 are 0000:0000 and collapse to ::. The compressed result is 2001:db8::8:800:200c:417a, exactly the canonical example from RFC 4291. To expand, do the reverse: the :: stands for however many zero groups are missing to reach 8 (here, two), and each group is padded back to 4 digits.

Expandido / Expanded:  2001:0db8:0000:0000:0008:0800:200c:417a
Comprimido / Compressed: 2001:db8::8:800:200c:417a

// Sub-rede / Subnet 2001:db8::/64
//   primeiro / first: 2001:db8::
//   último   / last : 2001:db8::ffff:ffff:ffff:ffff
//   total: 2^64 endereços de interface / interface addresses
A /64 fixes the 64 prefix bits (2001:db8:0:0) and leaves the final 64 bits for the interface identifier, 2^64 addresses in a single subnet.
  • Confirm the host bit count: 32 − prefix (IPv4) or 128 − prefix (IPv6).
  • Find the network with the AND between IP and mask; the broadcast is the top of the block.
  • Remember the minus two in IPv4, except /31 and /32.
  • When compressing IPv6, use :: only once, on the longest run of zeros.
  • Keep host subnets at /64 in IPv6 so you do not break SLAAC.
Type 192.168.10.77/26 or the IPv6 address above: the calculator shows network, broadcast, mask, binary and the IPv6 expansion, all in the browser, sending nothing.Open the tool full page

With the binary foundation in place, the next step is splitting a real network of different sizes with no gaps and no overlap, exactly what subnetting with VLSM in practice shows, step by step. To find your own prefix and address right now, see My IP; to geolocate any address, IP Locator.

Frequently asked questions

Why does a /24 have 254 hosts and not 256?
Because of the 256 addresses in the block, the first is the network address and the last is the broadcast; neither can be assigned to a host, leaving 254 usable. It is the 2^(32 − prefix) − 2 rule.
How does the subnet mask actually work?
The mask is a run of 1s (network bits) followed by 0s (host bits). The device performs a bitwise logical AND between the IP and the mask: where the mask is 1, the IP bit passes; where it is 0, the result zeroes. That produces the network address. It is not a memorized table, it is binary arithmetic.
Why did CIDR replace Classes A, B and C?
Classes offered only three sizes (256, 65 thousand or 16 million hosts), which wasted addresses and bloated the routing table. CIDR (RFC 1519, from 1993, updated by RFC 4632) allowed any prefix and hierarchical route aggregation, containing IPv4 exhaustion and the growth of routing tables.
Does IPv6 have broadcast?
No. IPv6 dropped broadcast and uses multicast for the same functions, for example, ff02::1 reaches all nodes on the link and ff02::2 all routers. This cuts unnecessary traffic and is one of the most visible differences from IPv4.
Why are IPv6 subnets almost always /64?
Because stateless autoconfiguration (SLAAC, RFC 4862) requires 64 bits for the interface identifier, and RFC 4291 fixes that size for most unicast addresses. A prefix longer than /64 on a host network breaks SLAAC. You vary the prefix only when carving larger blocks across sites, such as a /48 or /56.
Is IPv4 NAT a security measure?
No. NAT exists to save IPv4 addresses, not to protect the network. What blocks inbound connections is the firewall, with or without NAT. IPv6 drops NAT because it has addresses to spare; protection remains the job of a firewall with a default-deny inbound policy.

A mask is arithmetic, not memorization: the network address is the AND between IP and mask, the broadcast is the OR with the wildcard, and usable hosts are 2^(32 − prefix) − 2 (except /31 and /32). CIDR was born to end the waste of Classes A/B/C and slow the routing table; IPv6, with 128 bits, drops broadcast and NAT and adopts the /64 because of SLAAC. With the binary understood here, move on to practical planning with VLSM.

Sources & references

  1. RFC 4632, Classless Inter-domain Routing (CIDR), BCP 122
  2. RFC 1519, CIDR: the original specification (1993)
  3. RFC 1918, Private IPv4 addresses, BCP 5
  4. RFC 4291, IP Version 6 Addressing Architecture
  5. RFC 4193, Unique Local IPv6 Unicast Addresses (fc00::/7)
  6. RFC 4862, IPv6 Stateless Address Autoconfiguration (SLAAC)
  7. RFC 3021, Using 31-Bit Prefixes on IPv4 Point-to-Point Links