Show contents
Chapter 1 of 7 · 20 min
IPv4 addresses
By the end of this chapter, you'll be able to
- Read an IPv4 address in decimal and in binary
- Separate the network part and the host part using the mask
- Write a mask in CIDR notation
- Tell private addresses from public addresses
Four bytes
An IPv4 address is a 32-bit number. To make it readable, it is split into four bytes (8 bits each), written in decimal and separated by dots: 192.168.1.25. Each byte therefore goes from 0 to 255.
address = "192.168.1.25"
for byte in address.split("."):
print(byte.rjust(3), "=", format(int(byte), "08b"))
192 = 11000000
168 = 10101000
1 = 00000001
25 = 00011001
format(n, "08b") writes the number in binary on 8 digits. The computer itself only sees the sequence of 32 bits.
Network part and host part
An IP address holds two pieces of information, like a postal address holds the street and the house number:
- the network part: the network the machine belongs to (the street);
- the host part: the machine within that network (the number).
The subnet mask says where the boundary is. It is a 32-bit number that starts with 1s (the network part) followed by 0s (the host part). The mask 255.255.255.0 is written in binary as 24 ones then 8 zeros. It is shortened in CIDR notation: /24, the number of bits in the network part.
To find the network address, you do a bitwise logical AND between the address and the mask: a bit stays 1 only if it is 1 in both.
With the inverted mask, we get the host part: machine number 25 in this network.
Text description of the animation
- We look for the network address of 192.168.1.25 with the mask 255.255.255.0 (/24).
- The address in binary: 192.168.1.25.
- The mask: 24 bits set to 1, then 8 bits set to 0.
- Bitwise AND: wherever the mask is 1, we keep the address bit; elsewhere, we put 0. Result: 192.168.1.0.
- With the inverted mask, we get the host part: machine number 25 in this network.
Python does this computation for you with the standard ipaddress module:
import ipaddress
interface = ipaddress.ip_interface("192.168.1.25/24")
print(interface.ip)
print(interface.netmask)
print(interface.network)
192.168.1.25
255.255.255.0
192.168.1.0/24
Private and public addresses
There are only about 4.3 billion IPv4 addresses (2 to the power of 32), far fewer than connected devices. Three ranges are therefore reserved for private networks (homes, schools, companies) by RFC 1918. They are never routed on the Internet, and every private network can reuse them:
| Range | In CIDR | Common use |
|---|---|---|
| 10.0.0.0 to 10.255.255.255 | 10.0.0.0/8 | large companies |
| 172.16.0.0 to 172.31.255.255 | 172.16.0.0/12 | medium networks |
| 192.168.0.0 to 192.168.255.255 | 192.168.0.0/16 | Internet boxes, small networks |
All other addresses (except a few special ranges) are public: unique in the world and allocated by the Internet registries, such as AFRINIC for Africa.
import ipaddress
for text in ["192.168.1.25", "10.4.0.7", "172.20.1.1", "8.8.8.8", "196.200.60.1"]:
ip = ipaddress.ip_address(text)
print(text.ljust(13), "private" if ip.is_private else "public")
192.168.1.25 private
10.4.0.7 private
172.20.1.1 private
8.8.8.8 public
196.200.60.1 public
Mini-lab: taking an address apartPython 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Your turn
Worked exercise
Worked exercise: an address's card
Write a function card(text) that receives an address with its prefix, for example "172.20.14.9/20", and prints the address, the mask in decimal, the CIDR prefix, the network address and whether the address is private.
Hint 1
ipaddress.ip_interface(text) gives access to .ip, .netmask and .network.Hint 2
interface.network.prefixlen.Show the solution and common mistakes
import ipaddress
def card(text):
interface = ipaddress.ip_interface(text)
print("address:", interface.ip)
print("mask :", interface.netmask, f"(/{interface.network.prefixlen})")
print("network:", interface.network.network_address)
print("private:", "yes" if interface.ip.is_private else "no")
card("172.20.14.9/20")
address: 172.20.14.9
mask : 255.255.240.0 (/20)
network: 172.20.0.0
private: yes
With a /20 mask, the third byte is partly masked: 14 in binary is 00001110, and the mask 240 is 11110000. The AND gives 0, hence the network 172.20.0.0.
Common mistakes:
- Using
ip_addressinstead ofip_interface:ip_addressrefuses the/20, because a bare address has no mask. - Thinking that the network of a
/20always ends with.0.0: true here, but not for172.20.30.9/20, whose network is172.20.16.0.
Summary
- 1An IPv4 address has 32 bits, written as four decimal bytes from 0 to 255.
- 2The mask separates the network part (bits set to 1) from the host part (bits set to 0).
- 3CIDR notation /24 gives the number of bits in the network part.
- 4The network address is the logical AND of the address and the mask.
- 5The ranges 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 are private and reusable.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.