Um momento
0x20Lesson 3 of 7

Read IPv4 addresses and prefixes

Use a network prefix to identify addresses in the same subnet.

15 min 4-question quiz 1 code exercise
By the end of this lesson you can
  • Calculate a network address from an IPv4 address and CIDR prefix.

An IPv4 address is 32 bits. CIDR notation appends a prefix length such as /24 to say how many leading bits identify the network. Applying the prefix mask to an address gives the network address. Hosts use their prefix and a route to decide whether a destination is local or needs a gateway.

network_demo.py
1address = "192.168.4.27"
2prefix = 24
3octets = [int(part) for part in address.split(".")]
4value = 0
5for octet in octets:
6    value = (value << 8) | octet
7mask = (0xffffffff << (32 - prefix)) & 0xffffffff
8network = value & mask
9print(".".join(str((network >> shift) & 255) for shift in (24, 16, 8, 0)))
Output
192.168.4.0

CIDR replaced older class-based assumptions with explicit prefix lengths. A /24 leaves 8 bits for addresses within the block, while a /16 leaves 16. This exercise treats the input as valid IPv4; production software should validate every octet and prefix.

Key takeaways

  • Calculate a network address from an IPv4 address and CIDR prefix.

  • The code examples simulate protocol behavior; they do not send network traffic.

  • Use the layer model to locate where an address, port, or message belongs.

Lesson quiz

4 questions · pass with 3 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: model networking concepts

Use small Python programs to model packet, address, and protocol behavior. These exercises run locally in your browser and do not send network traffic.

Exercise 1

Find a /24 network address

+25 XP

Read an IPv4 address and print the network address for a /24 prefix. For this exercise, assume a valid address.

  • Host in private subnet
  • Another subnet
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: