IPv4 addressing and address classes
Your phone says “IP address: 192.168.1.105”. These four numbers are not random: they are really 32 zeros and ones, with the answers to “which network” and “which device” hidden inside. In today’s practical class you will convert an IPv4 address to binary by hand, determine the address class and type (private, public, special), and then automate the same work with Python’s ipaddress module.
Lesson goals and plan
By the end of the lesson you will…
- you can explain the structure of an IPv4 address (32 bits, 4 octets, network and host parts)
- you can convert octets from decimal to binary and back, both by hand and in Python
- you can determine classes A, B, C, D, E and the default mask from the first octet
- you can tell private (RFC 1918), public, loopback, APIPA and other special addresses apart
- you can calculate the network address, broadcast address and number of hosts for a classful address
- you can read Windows ipconfig output and analyze your computer’s address
Plan (according to the syllabus)
- IPv4 addressing and address classes
To avoid mistakes in binary, memorize one “magic row”: 128 64 32 16 8 4 2 1. You write every octet as a sum of these weights. Today you will need this row at least 50 times.
The first is a private address on your home network: this very address is used in millions of homes around the world at the same time. The second is Google’s DNS server, a public address, unique worldwide. How can you tell them apart? You have to “open” the address and look at the bits inside. Today you will learn to see an IPv4 address like an X-ray.
Key terms
Theory reminder
An IPv4 address is a 32-bit number. For people, we split it into 4 chunks of 8 bits (octets), convert each to a decimal number, and write them separated by dots. The total number of possible addresses: 232 = 4 294 967 296 (≈ 4.3 billion) — not enough for today’s number of devices, which is why IPv6 (next practical class) and NAT (Lecture 15) appeared.
| Bit position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Weight | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 192 = 128+64 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 168 = 128+32+8 | 1 | 0 | 1 | 0 | 1 | 0 | 0 | 0 |
| Class | Leading bits | 1st octet | Default mask | Networks | Hosts/network | Role |
|---|---|---|---|---|---|---|
| A | 0… | 1–126 | 255.0.0.0 (/8) | 126 | 16 777 214 | very large networks |
| B | 10… | 128–191 | 255.255.0.0 (/16) | 16 384 | 65 534 | medium networks |
| C | 110… | 192–223 | 255.255.255.0 (/24) | 2 097 152 | 254 | small networks |
| D | 1110… | 224–239 | — | — | — | multicast (sending to a group) |
| E | 1111… | 240–255 | — | — | — | reserved for experiments |
0.x.x.x is reserved for “this network”, 127.x.x.x is loopback; so class A is in practice 1–126. Number of hosts = 2host bits − 2 (the network address and the broadcast address are not assigned).
| Range | Name | Where found |
|---|---|---|
| 10.0.0.0 – 10.255.255.255 (10.0.0.0/8) | Private (A) | large enterprises, ISP internal networks |
| 172.16.0.0 – 172.31.255.255 (172.16.0.0/12) | Private (B) | corporate networks |
| 192.168.0.0 – 192.168.255.255 (192.168.0.0/16) | Private (C) | home and small-office routers |
| 127.0.0.0/8 | Loopback | localhost, developers’ server |
| 169.254.0.0/16 | APIPA (link-local) | when DHCP does not respond |
| 255.255.255.255 | Limited broadcast | DHCP Discover (Lecture 14) |
| 0.0.0.0 | “Unknown / any” address | the default route in the routing table |
Python: the ipaddress module — cheat sheetimport ipaddress ip = ipaddress.ip_address("192.168.1.105") int(ip) # 3232235881 — the address as a single number ip.packed # b'\xc0\xa8\x01i' — 4 bytes ip.is_private, ip.is_loopback # (True, False) format(192, "08b") # '11000000' — the octet as 8-digit binary int("10101000", 2) # 168 — binary to decimal ipaddress.ip_network("172.20.45.9/16", strict=False) # IPv4Network('172.20.0.0/16')
IPv4 addressing and address classes
We will do three sample tasks step by step: decimal to binary, binary to decimal, and a full analysis of a classful address. First on paper, then in Python.
Sample task 1. Convert 192.168.10.37 to binary
- Split the address into octets
192 | 168 | 10 | 37 — each is turned into its own 8 bits.
- “Subtraction” method: try the weights from the left
37: 128? no (0). 64? no (0). 32? yes → 1, remainder 5. 16? 0. 8? 0. 4? yes → 1, remainder 1. 2? 0. 1? yes → 1. Result:
00100101. - The remaining octets
192 = 128+64 →
11000000; 168 = 128+32+8 →10101000; 10 = 8+2 →00001010. - Every octet must be exactly 8 digits
Do not drop the leading zeros! Not
1010but00001010. Answer:11000000.10101000.00001010.00100101.
Sample task 2. 10101100.00010000.11111110.00000001 → decimal
- In each octet, add up the weights where there is a 1
10101100= 128+32+8+4 = 172;00010000= 16;11111110= 255−1 = 254;00000001= 1. - Result and check
172.16.254.1. This is in the 172.16.0.0/12 range — a private address (class B).
- A shortcut for quick calculation
If it is “almost all ones”, like
11111110, subtract the missing bits from 255: 255 − 1 = 254.11110000= 255 − 15 = 240.
Sample task 3. 172.20.45.9 — classful analysis
- Class
1st octet 172 → in the range 128–191 → class B (its bits start with
10…: 172 =10101100). - Default mask and parts
Class B → 255.255.0.0 (/16). The network part is 172.20, the host part is 45.9.
- Network and broadcast address
Fill the host bits with 0 → network 172.20.0.0; with all 1 → broadcast 172.20.255.255.
- Number of hosts
16 host bits → 216 − 2 = 65 534. The first host is 172.20.0.1, the last is 172.20.255.254.
Lab: the 32-bit IP “X-ray”
Click each bit to set it to 0/1, or type an address in the field. The widget shows the address in dotted decimal, its class (leading bits are colored), the network/host parts by the default mask, and the address type. In “Practice mode” you assemble the given address from bits yourself.
:ik-qidiruv: IPv4 X-ray
Read the address on your computer
C:\Users\Talaba> ipconfig Wireless LAN adapter Wi-Fi: Connection-specific DNS Suffix . : Link-local IPv6 Address . . . . . : fe80::4d2a:91c3:7b1e:2f10%12 IPv4 Address. . . . . . . . . . . : 192.168.1.105 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.1.1 Ethernet adapter Ethernet: Media State . . . . . . . . . . . : Media disconnected Ethernet adapter VirtualBox Host-Only Network: IPv4 Address. . . . . . . . . . . : 169.254.83.17 Subnet Mask . . . . . . . . . . . : 255.255.0.0
Independent tasks
7 tasks, from easy to hard. Write input() without a prompt text. In each task, the Check button tests your solution with several addresses. You may use the ipaddress module, but try writing tasks 1–4 by hand first (with format/int). The words your program prints (for example “Xato”, “A sinf”) stay in Uzbek, because the tests expect exactly them.
Common mistakes
| Error | Consequence | Correct answer |
|---|---|---|
10 → 1010 (4 digits) | you do not get 32 bits, and further calculations break | Every octet is 8 digits: 00001010; Python’da format(n, "08b") |
| Treating 172.32.x.x as private | wrong analysis | Private is only 172.16–172.31 |
| Giving 127.x.x.x to a class A host | the device will not reach the network | 127/8 is loopback only |
| Calculating the number of hosts as 2n | 2 extra addresses | 2n − 2: the network and broadcast addresses are not given to hosts |
int("10101000") | 10 101 000 — that is a decimal number! | int("10101000", 2) → 168 |
| Not splitting the string by dots and not converting to numbers | "192" > "50" — False (string comparison!) | Convert with int(o) |
Boss level
Boss level: “IP passport” generator
Write a helper for a network administrator. Input: a valid IPv4 address. Print (in exactly this order and format):
Manzil: 192.168.1.105 Ikkilik: 11000000.10101000.00000001.01101001 Sinf: C Turi: Xususiy Standart niqob: 255.255.255.0 Tarmoq: 192.168.1.0
Rules: Sinf (class) — A/B/C/D/E (1st octet 0 and 127 are also written as A). Turi (type) — in the order of task 6 (Loopback, APIPA, Xususiy, Multicast, Umumiy). For classes D and E print “Standart niqob: yo‘q” and “Tarmoq: yo‘q”. +50 XP
Reinforcement
Review questions
How many bits is an IPv4 address and how is it written?
32 bits; split into 4 octets, each written as a decimal number 0–255, separated by dots.
How do you convert an octet from decimal to binary?
Try the weights 128, 64, 32, 16, 8, 4, 2, 1 from the left: if a weight fits, write 1 and subtract it; the result is always 8 digits.
How are address classes determined?
By the first octet (leading bits): A 1–126 (0…), B 128–191 (10…), C 192–223 (110…), D 224–239, E 240–255.
What are the default masks and numbers of hosts of classes A, B, C?
A: 255.0.0.0, 16 777 214; B: 255.255.0.0, 65 534; C: 255.255.255.0, 254.
Name the private address ranges. Why are they needed?
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16. They are not routed on the Internet; they can be reused freely in local networks, which eases the IPv4 shortage.
What are loopback and APIPA addresses used for?
127.0.0.1 — addressing the device itself (checking the TCP/IP stack). 169.254.x.x — a link-local address assigned automatically when DHCP is not found.
Why is the number of hosts calculated with the formula 2n − 2?
The address with all host bits 0 is the network address, and the one with all host bits 1 is the broadcast address; they are not assigned to devices.
What was the drawback of classful addressing?
Addresses were wasted: for an organization with 300 hosts, C (254) is not enough, and B (65 534) is far too much. That is why subnetting and CIDR appeared (practical classes 3–4, 8–9).
Independent work
The independent-study topic in the syllabus: “IPv4 addressing, subnetting and efficient planning of IP addresses” (6 hours). The part relevant to this class is analyzing IPv4 addresses.
- For each of the 3 addresses in your variant, build a table by hand: binary form, class, type, default mask, network and broadcast address, number of hosts.
- Check the results with your “IP passport” program (screenshot).
- Run
ipconfig /allon your own computer and analyze the addresses of all adapters using the same table.
| Criterion | Points |
|---|---|
| Binary conversions without mistakes (by hand) | 2 |
| Class, type, mask, network/broadcast correctly determined | 1 |
| Checked with a Python program | 1 |
| Analysis of your own computer’s ipconfig /all | 1 |
Summary
Your lesson results
Your results are saved in this browser. Read all the sections and complete the exercises and assignments.