IP Addressing and Subnetting: Classes, Masks, CIDR and Hosts

IP addressing questions appear in computer papers for programmer, network administrator, lecturer and bank IT officer posts, and they range from naming the class of an address to finding the network and broadcast address of a subnet. All of them rest on one idea: an IPv4 address is 32 bits, split into a network part and a host part by the subnet mask. This lesson teaches a fast block-size method for subnetting and ends with the basics of IPv6. Every subnet answer here was checked with a program.

Asked in: PPSC, FPSC, NTS, ETEA, KPPSC, SSC, IBPS and ICS/ECAT computer

IPv4 addresses and the five classes

An IPv4 address is 32 bits long and is written as four decimal numbers called octets, each from 0 to 255, separated by dots, as in 192.168.1.10. Part of the address identifies the network and the rest identifies a host on that network.

In the original classful scheme, the first octet decides the class. Class A starts with 1 to 126, Class B with 128 to 191 and Class C with 192 to 223; these three are used for ordinary hosts. Class D (224 to 239) is reserved for multicast, where one sender reaches a group of receivers, and Class E (240 to 255) is reserved for research. Addresses beginning with 127 are kept for loopback testing: 127.0.0.1 always means this computer itself.

Each of the host classes has a default mask: Class A 255.0.0.0 (first octet is network), Class B 255.255.0.0 (first two octets), Class C 255.255.255.0 (first three octets).

Private and special addresses

Three blocks are set aside as private addresses. They are used freely inside homes, offices and campuses but are never routed on the public Internet; a router doing network address translation (NAT) lets many private devices share one public address.

The private blocks are 10.0.0.0 to 10.255.255.255 (10.0.0.0/8), 172.16.0.0 to 172.31.255.255 (172.16.0.0/12) and 192.168.0.0 to 192.168.255.255 (192.168.0.0/16). Note the middle one carefully: 172.16 to 172.31 is private, but 172.15 or 172.32 is not. A computer that cannot reach a DHCP server may give itself an automatic link-local address from 169.254.0.0/16.

Subnet masks and CIDR notation

A subnet mask has 1s in every network bit followed by 0s in every host bit. CIDR notation (Classless Inter-Domain Routing) simply writes the number of 1s after a slash: 255.255.255.0 is /24 because it has 24 ones. CIDR lets a network use any prefix length instead of only /8, /16 or /24.

Within one octet, the mask values follow a fixed pattern as 1s are added from the left: 128, 192, 224, 240, 248, 252, 254, 255 for 1 to 8 ones. So /26 is 255.255.255.192 (24 + 2 ones), /28 is 255.255.255.240 and /21 is 255.255.248.0 (16 + 5 ones).

Counting addresses and hosts

If the prefix is /n, the host part has 32 − n bits, so the subnet holds 2 raised to (32 − n) addresses. Two of them cannot be given to hosts: the first (all host bits 0) is the network address and the last (all host bits 1) is the broadcast address. The usable hosts are everything in between.

To choose a prefix for a given number of hosts, find the smallest number of host bits h for which 2 raised to h, minus 2, is at least the number needed. For 50 hosts, h = 6 gives 62, so use /26; for 500 hosts, h = 9 gives 510, so use /23. To count how many smaller subnets fit inside a larger one, raise 2 to the number of borrowed bits: a /24 split into /27s gives 2 raised to 3 = 8 subnets.

The block-size method for network and broadcast addresses

Find the interesting octet, the one where the mask is neither 255 nor 0. The block size is 256 minus the mask value in that octet. Subnets in that octet start at 0 and go up in steps of the block size. The network address is the largest multiple of the block size that does not exceed the address’s value in that octet; the broadcast address is the next network address minus one.

Example: 192.168.10.77/26. The mask is 255.255.255.192, so the block size is 256 − 192 = 64 and subnets start at 0, 64, 128, 192. 77 lies in the block starting at 64, so the network is 192.168.10.64 and the broadcast is 192.168.10.127. Usable hosts run from .65 to .126. Octets after the interesting one are 0 in the network address and 255 in the broadcast address.

IPv6 basics

IPv6 was created because the roughly 4.3 billion IPv4 addresses ran short. An IPv6 address is 128 bits, written as eight groups of four hexadecimal digits separated by colons, for example 2001:0db8:0000:0000:0000:0000:0000:0001.

Two rules shorten it. Leading zeros inside any group may be dropped (0db8 becomes db8, 0000 becomes 0). One run of consecutive all-zero groups may be replaced by a double colon, but the double colon may appear only once, otherwise the address would be ambiguous. So the example becomes 2001:db8::1, and the IPv6 loopback address is ::1. IPv6 has no broadcast; it uses multicast instead, and it does not need NAT because addresses are plentiful.

Solved examples

  1. Hamza’s office printer has the address 150.40.12.9 and uses its default mask. What class is it, and what is the mask?
    1. The first octet is 150, which lies between 128 and 191.
    2. So the address is Class B, whose first two octets form the network part.
    3. The default Class B mask is 255.255.0.0, which is /16.
    Answer: Class B, mask 255.255.0.0 (/16)
  2. Write the CIDR prefix /21 as a dotted-decimal subnet mask.
    1. 21 ones fill the first two octets (16 ones) and leave 5 ones for the third octet.
    2. Five ones from the left in an octet give 128 + 64 + 32 + 16 + 8 = 248.
    3. The fourth octet has no ones, so it is 0.
    Answer: 255.255.248.0
  3. A camera at Priya’s school has the address 192.168.1.100/28. Find the network address, broadcast address, host range and number of usable hosts.
    1. /28 means the mask is 255.255.255.240, so the block size is 256 − 240 = 16.
    2. Blocks start at 0, 16, 32, 48, 64, 80, 96, 112… and 100 falls in the block starting at 96.
    3. Network 192.168.1.96, broadcast 192.168.1.111 (112 − 1), hosts .97 to .110.
    4. Host bits = 32 − 28 = 4, so usable hosts = 2⁴ − 2 = 14.
    Answer: Network .96, broadcast .111, hosts .97–.110, 14 hosts
  4. Ali must split 192.168.1.0/24 into four equal subnets. What prefix should he use, and what are the subnets?
    1. Four subnets need 2 borrowed bits, since 2² = 4, so the prefix becomes 24 + 2 = /26.
    2. The mask is 255.255.255.192, so the block size is 64.
    3. Subnets start at 0, 64, 128 and 192, and each has 62 usable hosts.
    Answer: /26: 192.168.1.0, .64, .128 and .192
  5. Shorten the IPv6 address fe80:0000:0000:0000:0204:00ff:fe00:0012 as much as the rules allow.
    1. Drop leading zeros in every group: fe80:0:0:0:204:ff:fe00:12.
    2. Replace the single run of three zero groups with a double colon, which is allowed once.
    3. The groups after the run are kept as they are.
    Answer: fe80::204:ff:fe00:12
  6. A host on a large campus network has the address 10.20.30.40/12. Find its network and broadcast addresses.
    1. /12 means 8 ones in the first octet and 4 in the second, so the mask is 255.240.0.0; the interesting octet is the second one.
    2. Block size = 256 − 240 = 16, so blocks in the second octet start at 0, 16, 32… and 20 falls in the block starting at 16.
    3. Network address: 10.16.0.0. The next block starts at 10.32.0.0, so the broadcast is 10.31.255.255.
    Answer: Network 10.16.0.0, broadcast 10.31.255.255

Exam shortcuts

Common mistakes

Practice MCQs

  1. To which class does the IPv4 address 200.15.8.1 belong?
    A) Class A
    B) Class B
    C) Class C
    D) Class D
    Answer: C) Class C — A first octet of 200 lies in 192 to 223, the Class C range. Class A starts with 1 to 126, Class B with 128 to 191, and Class D (224 to 239) is reserved for multicast.
  2. Which of these IPv4 addresses is a private address?
    A) 172.20.5.9
    B) 172.32.1.1
    C) 192.169.1.1
    D) 11.0.0.1
    Answer: A) 172.20.5.9 — 172.20.5.9 lies inside the private block 172.16.0.0 to 172.31.255.255. 172.32.1.1 is just past that block, 192.169.1.1 is outside 192.168.0.0/16, and 11.0.0.1 is outside 10.0.0.0/8, so all three are public.
  3. What is the default subnet mask of a Class B network?
    A) 255.0.0.0
    B) 255.255.0.0
    C) 255.255.255.0
    D) 255.255.255.255
    Answer: B) 255.255.0.0 — In Class B the first two octets form the network part, so the default mask is 255.255.0.0 (/16). 255.0.0.0 is the Class A default, 255.255.255.0 is the Class C default, and 255.255.255.255 leaves no host bits at all.
  4. Which dotted-decimal subnet mask is the same as the prefix /26?
    A) 255.255.255.192
    B) 255.255.255.128
    C) 255.255.255.224
    D) 255.255.255.26
    Answer: A) 255.255.255.192 — /26 is 24 ones plus 2 more ones in the last octet, and two ones from the left give 128 + 64 = 192. 255.255.255.128 is /25, 255.255.255.224 is /27, and writing 26 inside the mask confuses the prefix length with an octet value.
  5. How many usable host addresses does a /27 subnet provide?
    A) 32
    B) 62
    C) 14
    D) 30
    Answer: D) 30 — A /27 leaves 32 − 27 = 5 host bits, giving 2⁵ = 32 addresses; removing the network and broadcast addresses leaves 30 usable hosts. 32 forgets that subtraction, 62 belongs to a /26 and 14 to a /28.
  6. The subnet mask 255.255.255.240 is written in CIDR notation as:
    A) /24
    B) /27
    C) /28
    D) /30
    Answer: C) /28 — 255.255.255 gives 24 ones and 240 is 11110000, adding 4 more, so the prefix is /28. /24 ignores the last octet, /27 would be .224 and /30 would be .252.
  7. What is the network address of the host 192.168.10.77/26?
    A) 192.168.10.0
    B) 192.168.10.77
    C) 192.168.10.127
    D) 192.168.10.64
    Answer: D) 192.168.10.64 — For /26 the block size is 256 − 192 = 64, so blocks start at 0, 64, 128 and 192; 77 lies in the block that starts at 64. 192.168.10.0 is the start of the previous block, .77 is the host itself, and .127 is the broadcast address of this subnet.
  8. What is the broadcast address of the subnet containing 10.1.5.130/25?
    A) 10.1.5.127
    B) 10.1.5.255
    C) 10.1.5.128
    D) 10.1.255.255
    Answer: B) 10.1.5.255 — For /25 the block size is 128, so the last octet splits into 0–127 and 128–255; 130 lies in the upper block, whose broadcast is 10.1.5.255. 10.1.5.127 is the broadcast of the lower block, 10.1.5.128 is this subnet’s network address, and 10.1.255.255 would need a /16 mask.
  9. Which of these can be assigned to a host in the subnet 192.168.5.32/27?
    A) 192.168.5.45
    B) 192.168.5.32
    C) 192.168.5.63
    D) 192.168.5.64
    Answer: A) 192.168.5.45 — With block size 32 the subnet runs from .32 to .63, so usable hosts are .33 to .62 and .45 is one of them. .32 is the network address, .63 is the broadcast address, and .64 belongs to the next subnet.
  10. Bilal divides the network 192.168.1.0/24 into /27 subnets. How many subnets does he get?
    A) 4
    B) 27
    C) 8
    D) 32
    Answer: C) 8 — Going from /24 to /27 borrows 3 bits, and 2³ = 8 subnets. 4 would come from borrowing 2 bits (/26), 27 is just the prefix length, and 32 is the number of addresses in each /27 subnet, not the number of subnets.
  11. A computer lab at Kavya’s college has 50 computers. What is the longest prefix (the smallest subnet) that still gives every computer its own usable address?
    A) /25
    B) /26
    C) /27
    D) /28
    Answer: B) /26 — A /26 has 62 usable hosts, enough for 50, while the next smaller subnet, /27, has only 30 and /28 has only 14. A /25, with 126 hosts, would also work but wastes addresses, so it is not the longest prefix that fits.
  12. How many bits long is an IPv6 address?
    A) 32
    B) 64
    C) 256
    D) 128
    Answer: D) 128 — IPv6 uses 128-bit addresses written as eight groups of four hexadecimal digits. 32 bits is the IPv4 size, 64 bits is only the usual interface-identifier half of an IPv6 address, and 256 is not used for IP addresses.
  13. Which is a correct shortened form of the IPv6 address 2001:0db8:0000:0000:0000:0000:0000:0001?
    A) 2001:db8::1
    B) 2001:db8:1
    C) 2001::db8::1
    D) 2001:db8::0::1
    Answer: A) 2001:db8::1 — Dropping leading zeros gives 2001:db8:0:0:0:0:0:1, and the run of six zero groups becomes one double colon. 2001:db8:1 throws away the zero groups without marking them, and the two forms with two double colons are invalid because the double colon may be used only once.
  14. What is the network address of the host 172.16.45.200/20?
    A) 172.16.0.0
    B) 172.16.45.0
    C) 172.16.32.0
    D) 172.16.40.0
    Answer: C) 172.16.32.0 — For /20 the mask is 255.255.240.0, so the interesting octet is the third with block size 16; blocks start at 0, 16, 32, 48, and 45 lies in the block starting at 32, giving 172.16.32.0. 172.16.0.0 is the classful Class B network, 172.16.45.0 treats the mask as /24, and 40 is not a multiple of 16.
  15. What is the last usable host address in the network 192.168.100.0/22?
    A) 192.168.100.254
    B) 192.168.103.254
    C) 192.168.103.255
    D) 192.168.104.254
    Answer: B) 192.168.103.254 — For /22 the block size in the third octet is 256 − 252 = 4, so the network covers 100 to 103 and the broadcast is 192.168.103.255; the last host is one less, 192.168.103.254. 192.168.100.254 treats the network as a /24, .103.255 is the broadcast itself, and 104 is already the next network.

Questions

Are IP address classes still used?

Real networks now use classless addressing (CIDR) with any prefix length, but exams still ask about classes and default masks because they explain where /8, /16 and /24 came from. Know both.

Why are two addresses lost in every subnet?

The address with all host bits 0 names the subnet itself, and the address with all host bits 1 is the broadcast address that reaches every host in the subnet. Neither can be given to a single device.

What does 127.0.0.1 mean?

It is the loopback address. Traffic sent to it never leaves the computer, so it is used to test that the network software on the machine itself is working. In IPv6 the loopback address is ::1.

How do I find the block size quickly?

Subtract the mask value in the interesting octet from 256. For a mask ending in 224 the block is 32; for 240 it is 16; for 248 it is 8; for 252 it is 4.