Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 79 ยท The Network Layer: IP

IPv4 Addresses

Understand IPv4 addresses: dotted decimal and binary, historical classes, private and special ranges, loopback and link-local, with a live Python converter.

Every device that talks over the Internet needs an address, and for decades the address format has been the IPv4 address: a 32-bit number that identifies a network connection. Your Wi-Fi card has one, your phone has one on the mobile network, and every web server you reach has one. The data link layer you just finished moves frames between neighbors with MAC addresses; IP addresses are what let a packet cross many networks to a host on the other side of the world.

IPv4 addresses look simple, four numbers separated by dots, but a lot of design hides behind them: which part names the network and which the host, which blocks are reserved for private use, and what special addresses like 127.0.0.1 and 169.254.x.x mean. This lesson teaches the notation, the binary underneath, the historical class system that explains old vocabulary, and the reserved ranges you will meet in real networks. The next lesson, subnetting, builds on it.

Dotted decimal and binary

An IPv4 address is 32 bits long, giving 2^32 = 4,294,967,296 possible addresses. Nobody wants to write 32 bits, so we split them into four 8-bit groups called octets and write each as a decimal number from 0 to 255:

Text
11000000 10101000 00000001 00001010   binary
   192      168       1        10      dotted decimal

That is 192.168.1.10. Each octet is the value of 8 bits, so it can be read with the weights 128, 64, 32, 16, 8, 4, 2, 1. The first octet above is 128 + 64 = 192. This is why no octet can be 256 or larger, and why 300.1.1.1 is not an address.

The same address is also a single number. Treat the four octets as the digits of a base-256 number: 192 x 256^3 + 168 x 256^2 + 1 x 256 + 10 = 3,232,235,786. Programs and routers work with this integer, and many address calculations (like finding a subnet) are simple bitwise operations on it.

Two practical warnings:

  • Octets must be written in plain decimal without leading zeros in most modern software. Some old parsers treat 010 as octal (8), which has caused real security bugs.
  • IP addresses are in network byte order (big-endian) when sent inside a packet, with the first octet first.

Network part and host part

An IPv4 address has two parts. The leading bits are the network portion, shared by every host on the same network, and the remaining bits are the host portion, which picks one interface inside that network. Routers look only at the network portion to decide where to send a packet, which is how the Internet scales: a router needs one entry for a whole network, not one for every host.

The question is where the dividing line falls. Today a subnet mask or a prefix length says so (for example /24 means the first 24 bits are the network), and you will learn this in the next two lessons. Historically the answer was built into the address itself.

Classful addressing (historical)

In the original design, the first few bits of an address decided its class, and the class fixed the network/host split:

Class First bits First octet Network bits Host bits Hosts per network
A 0 0 to 127 8 24 16,777,214
B 10 128 to 191 16 16 65,534
C 110 192 to 223 24 8 254
D 1110 224 to 239 (multicast)
E 1111 240 to 255 (reserved)

The usable host count is 2^h - 2, because two host values are reserved in every network: all host bits 0 is the network address itself, and all host bits 1 is the directed broadcast address.

Classful addressing wasted enormous amounts of space. An organization needing 2,000 addresses was too big for a class C (254 hosts) and received a class B (65,534), leaving over 63,000 addresses unused. By the early 1990s the pool was running out, which led to CIDR in 1993 (see CIDR and VLSM), which dropped the classes. Classes no longer determine anything in real networks, but the vocabulary survives: people still say "a class C network" for a /24, and you will see it in exams and old documentation. Class D is still alive as the multicast range (multicast).

Default masks and the class test

Because the class was readable from the first bits, a classful router needed no mask in a routing update: it looked at the first octet and knew the prefix length. That is why early protocols such as RIP version 1 never carried a mask. The default mask of each class is 255.0.0.0 (A), 255.255.0.0 (B) and 255.255.255.0 (C).

A few details trip people up:

  • Class A starts at 0.0.0.0, but network 0 means "this network" and 127 is loopback, so only 126 class A networks (1 to 126) exist. There are 16,384 class B networks and 2,097,152 class C networks.
  • To see the class, look at the leading bits: 0 is A, 10 is B, 110 is C, 1110 is D (multicast), 1111 is E (reserved for experiments).
  • Only classes A, B and C hold ordinary host addresses. D and E have no network/host split at all.

The problems that led to CIDR

Three problems built up through the 1980s and early 1990s:

  1. Wasted addresses. Only three sizes existed (254, 65,534 or about 16.7 million hosts). A company with 300 hosts was too big for a class C and received a whole class B, wasting more than 99% of it.
  2. Running out of class B. Class B was the popular size, and by the early 1990s it was nearly exhausted.
  3. Routing table growth. The alternative to a class B was several class C networks, but each one needed its own route, so backbone routers' tables exploded.

Subnetting (RFC 950, 1985) already let an organization split its own class A, B or C block internally, but it could not fix any of the three problems from the outside. CIDR (RFC 1519, 1993) removed the classes altogether: any prefix length is allowed, blocks can be sized to fit, and neighboring blocks can be aggregated into one route. Read on in CIDR and VLSM.

The next example classifies addresses by their first octet and shows how much of the smallest fitting class an organization would leave unused.

Python Classify an address and count the waste

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Private addresses

There are not enough IPv4 addresses for every device, and most devices do not need to be reachable from the Internet. RFC 1918 reserves three blocks for private use. Anyone may use them inside their own network, and they are never routed on the public Internet:

Block Range Size
10.0.0.0/8 10.0.0.0 to 10.255.255.255 16.7 million addresses
172.16.0.0/12 172.16.0.0 to 172.31.255.255 1 million addresses
192.168.0.0/16 192.168.0.0 to 192.168.255.255 65,536 addresses

Your home router almost certainly gives your devices 192.168.x.x or 10.x.x.x addresses and uses NAT to share one public address among them. Note that the middle block ends at 172.31, not 172.255: a common mistake is to think 172.200.0.1 is private; it is a public address.

Because many organizations use the same private ranges, two networks that use 10.0.0.0/8 cannot be joined directly without renumbering or translation. This is one of the practical headaches of private addressing, especially after company mergers and with VPNs.

Special-purpose addresses

Several other blocks have fixed meanings. A network engineer should recognise them on sight.

Address / block Name Meaning
0.0.0.0 Unspecified "This host, no address yet." Used by a host before it has an address (for example when it starts DHCP), and in servers as "listen on all interfaces". 0.0.0.0/0 is the default route.
127.0.0.0/8 Loopback Traffic to 127.0.0.1 (localhost) never leaves the machine. Used to test local servers and software.
169.254.0.0/16 Link-local A host that cannot get an address from DHCP assigns itself one here. Packets with these addresses are not routed. Seeing 169.254.x.x means "DHCP failed".
100.64.0.0/10 Shared address space Used by ISPs for carrier-grade NAT, between a customer and the provider.
224.0.0.0/4 Multicast One packet delivered to a group of receivers.
255.255.255.255 Limited broadcast Delivered to every host on the local network, never forwarded by routers.
192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24 Documentation Reserved for examples in books and manuals. They are never real hosts.
198.18.0.0/15 Benchmarking Used for network performance tests.
240.0.0.0/4 Reserved (class E) Not usable on the public Internet.

Everything not covered by these blocks is public (globally routable) space, handed out through the regional registries (ARIN, RIPE NCC, APNIC, LACNIC and AFRINIC) to ISPs and organizations. Look at the structure of that hierarchy in the structure of the Internet.

Anything in the 127.0.0.0/8 block is loopback, not only 127.0.0.1, so 127.1.2.3 also stays on your machine. And the lowest and highest address of any subnet have roles of their own, which you will compute in the next lesson.

Run the converter

The Python below is a small address toolkit. It converts between dotted decimal, binary and the 32-bit integer, determines the historical class, and sorts any address into the categories above. It uses plain bit operations so you can see the arithmetic.

Python IPv4 converter and classifier

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice 172.31.255.1 is private but 172.32.0.1 is public: the second octet of the /12 block stops at 31.

Playing with the integer form

Because an address is a number, you can do arithmetic on it: add 1 for the next address, subtract two addresses to count how many lie between them, or AND it with a mask to find its network. The second example does exactly this for a small range of addresses, and shows why exactly 2^h - 2 hosts are usable in a classic network.

Python Counting addresses and finding network boundaries

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the dotted decimal form of the binary address 11000000.10101000.00000001.00000001?

    11000000 = 192, 10101000 = 168, 00000001 = 1 and 00000001 = 1.
  2. 2. Which of these addresses is NOT in a private (RFC 1918) range?

    The private block is 172.16.0.0/12, which ends at 172.31.255.255. 172.40.0.5 is public.
  3. 3. A laptop shows the address 169.254.33.8 and cannot reach anything. What is the most likely cause?

    169.254.0.0/16 is the link-local range hosts use when DHCP fails, so the host can talk only to neighbors on the same link.
  4. 4. How many usable host addresses does a classful class C network have?

    A class C network has 8 host bits, so 256 addresses; the all-zero network address and the all-one broadcast address cannot be assigned to hosts.
  5. 5. Why did the Internet abandon classful addressing?

    Organizations received blocks far larger than they needed. CIDR replaced classes with flexible prefix lengths.

Summary

  • An IPv4 address is 32 bits, written as four decimal octets. It can also be seen as one integer, which makes bitwise network calculations easy.
  • An address has a network part and a host part. Classful addressing fixed the split by the first bits (A, B, C, plus D multicast and E reserved), wasted space and was replaced by CIDR.
  • Private blocks are 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. They are reusable and not routed on the public Internet.
  • Special addresses include 0.0.0.0, loopback 127.0.0.0/8, link-local 169.254.0.0/16, multicast 224.0.0.0/4, limited broadcast 255.255.255.255 and the documentation blocks.
  • In every network the all-zero host part is the network address and the all-one host part is the broadcast address, so they are not usable by hosts.
  • A classful address is classified by its first octet alone: 1 to 126 is class A (/8), 128 to 191 class B (/16), 192 to 223 class C (/24); the one-size-fits-all blocks wasted space and bloated routing tables, which led to CIDR.

Next, see how masks divide an address block into smaller networks in subnetting.