Sub topic 5
Class ranges and default masks
This chapter assumes you can convert an octet between decimal and binary, and that you know an IPv4 address is a 32-bit number split into a network part and a host part. The previous chapters covered both. Here you will learn how the original classful scheme used the first bits of an address to decide where that split falls.
The idea: the first bits choose the class
In the classful scheme, a router did not need to be told where the network part ended. It looked at the leading bits of the address, and those bits fixed both the class and the size of the network part. The class gave a default mask, the mask a router assumed when no other mask was configured.
The rule is a prefix code. You read bits from the left until the pattern is unambiguous:
- A leading
0means class A. - A leading
10means class B. - A leading
110means class C. - A leading
1110means class D. - A leading
1111means class E.
Because the patterns are in this order, the class depends only on the first octet. You never need the other three.
Class ranges
The leading bits fix the lowest and highest possible first octet for each class. The remaining bits of the octet are free.
| Class | Leading bits | First octet range | Default mask | Prefix | Network bits | Host bits |
|---|---|---|---|---|---|---|
| A | `0` | 0 to 127 | 255.0.0.0 | /8 | 8 | 24 |
| B | `10` | 128 to 191 | 255.255.0.0 | /16 | 16 | 16 |
| C | `110` | 192 to 223 | 255.255.255.0 | /24 | 24 | 8 |
| D | `1110` | 224 to 239 | none | none | not applicable | not applicable |
| E | `1111` | 240 to 255 | none | none | not applicable | not applicable |
You can derive the ranges instead of memorizing them. Class B starts with 10, so the lowest first octet is 10000000, which is 128. The highest is 10111111, which is 191. Class C runs from 11000000 (192) to 11011111 (223). The same method gives 224 to 239 for class D.
Default masks
A default mask is the network part written as ones, followed by zeros for the host part. For class B, that is 16 ones and 16 zeros:
11111111.11111111.00000000.00000000 = 255.255.0.0Take the address 172.16.5.4. The first octet is 172, which falls in 128 to 191, so it is class B. The default mask is 255.255.0.0. A bitwise AND of the address with the mask gives the network address:
172.16.5.4 10101100.00010000.00000101.00000100
255.255.0.0 11111111.11111111.00000000.00000000
AND result 10101100.00010000.00000000.00000000 = 172.16.0.0So 172.16.5.4 is host 5.4 on network 172.16.0.0. Every class B address that starts with 172.16 is on that same network.
Networks and hosts per class
The number of host bits sets the size of each network. The number of network bits that are not fixed by the leading pattern sets how many networks exist. The formulas below use the classful convention that the all-zeros and all-ones host values are not usable, so each network loses two addresses.
| Class | Free network bits | Networks | Hosts per network |
|---|---|---|---|
| A | 7 | 126 | 16,777,214 |
| B | 14 | 16,384 | 65,534 |
| C | 21 | 2,097,152 | 254 |
Class A has 2^7 = 128 possible network numbers, but two are set aside. Network 0 means "this network" and network 127 is the loopback range. That leaves 126. Hosts per network are 2^24 − 2 = 16,777,214 for class A, 2^16 − 2 = 65,534 for class B, and 2^8 − 2 = 254 for class C.
The table shows the main weakness of the scheme. Sizes jump by a factor of 256 between classes. An organization with 300 hosts did not fit in a class C network (254 hosts), so it received a class B network with 65,534 addresses and used about half a percent of it. That waste is why classes were replaced by CIDR, which a later chapter covers.
Classes D and E have no default mask
Class D (224.0.0.0 to 239.255.255.255) is for multicast. A multicast address names a group of receivers, not a single host or a network of hosts. All 28 bits after the 1110 prefix form the group identifier, so there is no network and host split and no mask in the classful sense.
Class E (240.0.0.0 to 255.255.255.255) was reserved for experimental use and has never been assigned for normal traffic. The address 255.255.255.255 sits in this range and has a special meaning, the limited broadcast address. A later chapter covers special-purpose ranges.
Computing the class and default mask in Python
The function below finds the class from the first octet, as in the rest of this tutorial. It then builds the default network with the standard ipaddress module. Passing strict=False lets you give a host address and get back the network that contains it.
import ipaddress
DEFAULT_PREFIX = {"A": 8, "B": 16, "C": 24}
def ipv4_class(address):
first = int(address.split(".")[0])
if first < 128:
return "A"
if first < 192:
return "B"
if first < 224:
return "C"
if first < 240:
return "D"
return "E"
def default_network(address):
cls = ipv4_class(address)
if cls not in DEFAULT_PREFIX:
raise ValueError(f"class {cls} has no default mask")
prefix = DEFAULT_PREFIX[cls]
return ipaddress.ip_network(f"{address}/{prefix}", strict=False)
for text in ["10.1.2.3", "172.16.5.4", "192.168.0.1"]:
net = default_network(text)
print(text, ipv4_class(text), net.netmask, net.network_address, net.num_addresses - 2)The output is:
10.1.2.3 A 255.0.0.0 10.0.0.0 16777214
172.16.5.4 B 255.255.0.0 172.16.0.0 65534
192.168.0.1 C 255.255.255.0 192.168.0.0 254The num_addresses - 2 term subtracts the network and broadcast addresses, which matches the "hosts per network" column above.
You can also classify an address by its leading bits, which mirrors the diagram:
def class_from_bits(address):
first = int(address.split(".")[0])
bits = format(first, "08b")
if bits.startswith("0"):
return "A"
if bits.startswith("10"):
return "B"
if bits.startswith("110"):
return "C"
if bits.startswith("1110"):
return "D"
return "E"
print(class_from_bits("128.0.0.1")) # B
print(class_from_bits("191.255.0.1")) # B
print(class_from_bits("192.0.0.1")) # CBoth functions agree on every input, because the numeric ranges are just the leading-bit patterns written in decimal.
Common errors
Most mistakes come from treating the class boundaries as approximate. These are the ones to watch for.
- Off-by-one at a boundary. 127.x.x.x is still in the class A numeric range, but it is loopback and not a usable class A network. 191 is class B and 192 is class C. 223 is class C and 224 is class D.
- Using the second octet to decide the class. Only the first octet matters.
10.200.0.1is class A even though 200 would look like class C if you read the wrong octet. - Giving class D or E addresses a mask. A multicast address is not a host on a subnet. Assigning 224.0.0.5 to a host interface with a /24 mask is a configuration mistake.
- Forgetting the two reserved host values. The all-zeros host value names the network and the all-ones host value is the broadcast address. A class C network has 256 addresses but 254 usable hosts.
- Assuming classes still decide routing. Modern routers and operating systems carry an explicit mask or prefix length with every route. The default masks above are mostly a teaching tool and a fallback in a few legacy tools.
What comes next
Default masks are the starting point for subnet masks. Once you can say that a class B address has a /16 default mask, you can see why borrowing host bits to make a longer prefix gives smaller networks. The next chapters build on this with subnet masks, prefix lengths, and the classless scheme that removed the fixed class sizes.
Exercise
For each address, give the class, the default mask in dotted decimal, and the network address: (a) 9.200.15.3, (b) 130.5.77.1, (c) 205.10.20.30.
Show answer
(a) The first octet is 9, which is below 128, so it is class A. The default mask is 255.0.0.0 and the network address is 9.0.0.0. (b) The first octet is 130, in the range 128 to 191, so it is class B. The default mask is 255.255.0.0 and the network address is 130.5.0.0. (c) The first octet is 205, in the range 192 to 223, so it is class C. The default mask is 255.255.255.0 and the network address is 205.10.20.0.
Exercise
A company needs one network for 1,000 hosts under the classful scheme. Which class must it use, how many addresses go unused, and why?
Show answer
A class C network holds only 254 hosts, which is too few. A class B network holds 65,534 usable hosts, so the company must use class B. About 64,534 addresses go unused (65,534 minus 1,000). The classful scheme only offers three network sizes, so there is no size between 254 and 65,534 hosts.