A computer science question tests your ability to work with IP addresses and network masks

⏱️ Estimated Reading Time: ~13 min read
0
(0)

A computer science question tests your ability to work with IP addresses and network masks. This is a typical problem on the topic of “Computer Networks” and is regularly asked. You typically have about 3 minutes to complete this task, and you’ll receive one primary point for a correct answer.

 

What problems are found in issue number 13?

This task may contain questions in various formats, but they all revolve around network addresses and masks. Here are the main types of problems:

  • Calculating the number of addresses in a network. You need to determine how many IP addresses are included in a given network using a given mask. Some options may ask you to count the number of addresses available to nodes (that is, excluding the network address and broadcast address).
  • Determining the network mask. You’re given the host’s IP address and network address, and asked to find the subnet mask. Most often, they ask for a specific byte of the mask—for example, the third byte from the left.
  • Finding a network address or broadcast address. Using the IP address and mask, you need to determine the network address or the broadcast address.
  • Checking IP address network membership. Given an IP address and a network with a mask, the task is to determine whether this IP address is part of the specified network.

In this article, we’ll begin by analyzing a problem from the FIPI demo version—the official open version of the Unified State Exam. We’ll solve it step by step using the Python programming language, explaining each step in detail to make it as clear as possible, even for those just beginning to learn about networks.

And then we’ll go over other typical problems that might appear under question number 13, so you’re prepared for all the options on the exam.

Theory for solving the question

Task #13 on the Unified State Exam in Computer Science deals with networks and IP addresses. To successfully solve it, you need to understand how IP addresses work, what a network mask is, how an address is divided into a network and a host, and how to calculate a network address. None of this is too difficult if you understand the basic rules—we’ll explain them step by step in simple terms.

What is an IP address?

An IP address is a unique address assigned to every device on a computer network. It consists of four numbers from 0 to 255, separated by periods. For example:

192.168.1.5

Inside computers, this address is stored as a 32-bit binary number (that is, 4 groups of 8 bits, for a total of 32 bits).

Example:

192.168.1.5 → 11000000.10101000.00000001.00000101

What does an IP address consist of?

The IP address is divided into two parts:

  • Network address – shows which network the device belongs to.
  • The node (host) address is a unique number for a device within this network.

How do I find out where the network address ends and the host address begins?

A network mask is used for this. A network mask is also a 32-bit number that looks like an IP address. It indicates how many bits of the IP address are assigned to the network and how many are assigned to the hosts.

Example of a mask:

255.255.255.0 → 11111111.11111111.11111111.00000000
  • Where there are ones (1) – this is part of the network address.
  • Where there are zeros (0) – this is part of the host address.

How to get a network address?

To calculate the network address, you need to perform a bitwise conjunction (bitwise AND) between the IP address and the network mask. A bitwise conjunction is an operation that compares bits one at a time:

  • 1 and 1 → 1
  • 1 and 0 → 0
  • 0 and 1 → 0
  • 0 and 0 → 0

Example:

// IP-address
192.168.1.5    → 11000000.10101000.00000001.00000101
// Network mask
255.255.255.0  → 11111111.11111111.11111111.00000000

Let’s perform bitwise AND:

11000000.10101000.00000001.00000101
11111111.11111111.11111111.00000000
----------------------------------
11000000.10101000.00000001.00000000

This is the network address: 192.168.1.0.

How to determine the number of addresses in a network?

The number of addresses in a network depends on the number of zeros in the network mask. For example, if the mask contains 3 zeros, then the network has 2³ = 8 addresses.

Example: mask 255.255.255.248 (in binary form, this is 29 ones and 3 zeros) → there will be 2³ = 8 addresses in the network.

What types of addresses are there on the network?

Each network has:

  • Network address (the very first address) – shows the network itself.
  • Broadcast address (the last address) – used to send a message to all devices on the network.
  • Available IP addresses for devices are all addresses between the first and last.

Hint for question: Computer Science Exam

In questions, they often ask:

  • Calculate the number of addresses based on a condition (for example, where the number of units is not a multiple of 4).
  • Or find a range of IP addresses on the network.

To solve such problems:

  1. Find the number of addresses in the network (by the number of zeros in the mask).
  2. Loop through all IP addresses (this is easy to do in Python using the module. ipaddress).
  3. For each address, check the condition from the problem (for example, count the number of ones in its binary representation).

The main thing to remember:

  • IP address = network address + device address within the network.
  • The mask shows where the network address ends and the host address begins.
  • Network address = IP address bitwise AND network mask.
  • Number of addresses in the network = 2^(number of zeros in the mask).

Counting the number of addresses in a network

Problem statement: In TCP/IP networking terminology, a network mask is a binary number that indicates which portion of a host’s IP address is the network address and which portion is the host address within that network. The network address is obtained by applying a bitwise conjunction to the given host address and network mask.

The network is specified by the IP address 122.159.136.144 and the network mask 255.255.255.248.

How many IP addresses are there in this network for which the number of ones in the IP address’s binary notation is not a multiple of 4? Please provide only the number in your answer.

This problem can be solved very easily using the Python programming language. The entire solution can be conveniently presented in five simple steps.

To avoid manually calculating IP addresses, we’ll use a special module ipaddress. It’s already built into Python and can automatically find and work with all addresses on the network, so we don’t have to do any manual calculations.

Step 1. Connect the module and create a network.

Python
import ipaddress

network = ipaddress.ip_network('122.159.136.144/255.255.255.248')

Here we include the module ipaddressand create a network object. Using this function, ip_network()we tell Python, “Here’s an IP address and a mask – go ahead and calculate all the addresses on this network. “

Step 2. We go through all the IP addresses in this network.

Python
for ip in network:

This line starts a loop that iterates through each IP address on the network. In other words, the program itself “walks” through all the addresses one by one.

Step 3. Count the number of ones in the binary notation of the address.

Python
ones = bin(int(ip)).count('1')

Here we perform three actions at once:

  • First, we convert the IP address into a number: int(ip). For example, from the form 122.159.136.144it is converted into a large number.
  • Then we convert this number to binary using bin(). Binary is a representation of zeros and ones.
  • And finally, we use it .count('1')to count how many units are in this entry.

Step 4. Check the problem statement.

Python
if ones % 4 != 0:
    count += 1

According to the problem statement, we only need addresses where the number of ones is not divisible by 4. If the condition is met, we increase our counter countby 1.

Step 5. Display the result.

print(count)

When the loop has finished iterating through all the addresses, the program will display how many addresses matched the problem condition.

Final solution code:

Python
import ipaddress

# Specified IP and netmask
network = ipaddress.ip_network('122.159.136.144/255.255.255.248')

count = 0

for ip in network:
    ones = bin(int(ip)).count('1')
    if ones % 4 != 0:
        count += 1

print(count)

Answer: 5.

Definition of a mask

Problem statement: In TCP/IP networks, the “mask” indicates which part of the IP address is the network and which is the specific computer within it. The mask is written exactly like the IP address: four numbers from 0 to 255. In binary notation, the ones (the network portion) come first, followed by zeros (the host portion). To obtain the network address, perform a bitwise “AND” between the device’s IP address and the mask.

For example, if the IP address of a host is 10.14.35.78 and the mask is 255.255.248.0, then the network address is 10.14.32.0.

The computer’s IP address is 172.16.205.14, and the network address is 172.16.192.0. Find the third byte of the mask (counting from the left, in decimal form). Write the answer as a single number.

Before we get started, let’s understand how it works. We have two numbers: a byte from the IP address and a byte from the network address. Using these, we can easily reconstruct the network mask. We’ll do this step-by-step and as simply as possible:

  1. Let’s translate the required bytes into “machine language” – the binary system (zeros and ones).
  2. Let’s see how the bitwise AND operation works: only when both digits are 1 will the result be 1. In all other cases, it will be 0.
  3. Let’s determine which bits in the mask give the desired result.
  4. Let’s convert the resulting mask back into the usual decimal form.

Now let’s go step by step.

Step 1. Write the equation for bytes. For each byte, the following rule applies:

IP₃ & M₃ = N₃

Where:

  • IP3– the third byte of the IP address (in our case it is 205);
  • N3– the third byte of the network address (in our case it is 192);
  • M3– the third byte of the mask to be found.

Step 2. Convert the numbers to binary. Now let’s write our numbers as ones and zeros, just like a computer would:

  • 205 10 = 11001101 2
  • 192 10 = 11000000 2

This will help us in the next step to compare the bits and determine the mask.

Step 3. Break down the bitwise operation and find the mask. Let’s look at how the bitwise AND operation works:

  11001101   (IP₃)
& ????????   (M₃)
= 11000000   (N₃)

The first two bits from the left are 1 in both the IP address and the resulting mask, meaning they are also 1. All the remaining bits are 0 in the resulting mask, meaning those positions in the mask are zeros. The resulting mask looks like this:110000002

Step 4. Convert the mask to decimal. Now all that’s left is to convert our mask from binary notation back to its familiar decimal form.110000002 = 128 + 64 = 19210

That is, the third byte in the mask is the number 192.

Answer: The third byte of the mask is 192.

Finding a network address or broadcast address

Problem statement: In networks, the network address is determined using the device’s IP address and network mask. Simply apply a bitwise “AND” to each byte.

Find the network address for the following IP and mask.

  • IP address: 150.25.199.18
  • Mask: 255.255.224.0

In the table, find the numbers from the network address and write down the answer, indicating the corresponding letters.

A table of numbers for composing an answer based on the network address in Task 13 of the Unified State Exam in Computer Science (selecting bytes by letters)

Now let’s solve the problem using Python. We’ll use a built-in module ipaddress—it will automatically calculate the network address given an IP address and mask. Our task is simply to obtain the result and select the correct answer from the table.

Let’s break it down step by step:

Step 1. Run the code and find the network address.

Python
import ipaddress

# Set the IP address and network mask
network = ipaddress.ip_network('150.25.199.18/255.255.224.0', False)

# Displaying the network address
print(network.network_address)

Why do we specify FalseThis is necessary so that Python correctly interprets the node’s regular IP address, rather than requiring a predefined network address.

When we execute this code, the program will print the network address it found. In our example, it will be: 150.25.192.0.

Step 2. Break the network address down into individual numbers. The resulting network address is simply four numbers (bytes) separated by periods:

  • 150
  • 25
  • 192
  • 0

Step 3. Compile your answer using the table from the task. Now take each of these numbers and find the corresponding letter in the table. For the network address 150.25.192.0, it would be like this:

  • 150 → E
  • 25 → A
  • 192 → G
  • 0 → F

All that remains is to collect the letters in the same order as the bytes in the address: EAGF.

 Answer: EAGF.

Checking if an IP address is part of a network

Problem statement: In TCP/IP networking terminology, a network address is obtained by applying a bitwise conjunction (the “AND” operation) between the IP address and the network mask. To check whether a given IP address is part of a network, compare its network address with the network address specified in the problem statement.

The network is specified by the address 192.168.64.0 and mask 255.255.192.0. Determine whether the host with the IP address 192.168.95.23 is part of this network. If it is, write YES in the answer; if not, write NO.

Let’s break down the problem step by step and write a solution in Python.

Step 1. Convert the IP address and mask to binary.

Network IP address: 192.168.64.0

  • 192 → 11000000
  • 168 → 10101000
  • 64 → 01000000
  • 0 → 00000000

In binary: 11000000.10101000.01000000. 00000000

Network mask: 255.255.192.0

  • 255 → 11111111
  • 255 → 11111111
  • 192 → 11000000
  • 0 → 00000000

In binary: 11111111.11111111.11000000. 00000000

Node IP address: 192.168.95.23

  • 192 → 11000000
  • 168 → 10101000
  • 95 → 01011111
  • 23 → 00010111

In binary: 11000000.10101000.01011111.00010111

Step 2. Perform a bitwise AND operation between the network’s IP address and mask. To do this, compare the bits of the IP address and mask bit by bit. If the corresponding position in both the IP address and mask is a 1, the result is 1; otherwise, the result is 0.

Network IP address (in binary):

11000000.10101000.01000000.00000000

Netmask (in binary):

11111111.11111111.11000000.00000000

Result of the “AND” operation (network address):

11000000.10101000.01000000.00000000 (network address)

As we can see, the result matches the network IP address that was given in the problem – 192.168.64.0.

Step 3. Apply the bitwise AND operation to the node’s IP address.

Node IP address (in binary):

11000000.10101000.01011111.00010111

Netmask (in binary):

11111111.11111111.11000000.00000000

The result of the “AND” operation:

11000000.10101000.01000000.00000000 (network address, which must match the network)

As we can see, the result matches the network address 192.168.64.0; that is, node 192.168.95.23 is located in this network.

Step 4. Write the solution in Python.

Python
import ipaddress

#Specified parameters
network = ipaddress.ip_network('192.168.64.0/255.255.192.0', strict=False)
ip = ipaddress.ip_address('192.168.95.23')

#Checking if the IP is on the network
if ip in network:
    print("YES")
else:
    print("НЕТ")

The result of executing the program will be YES, since the IP address 192.168.95.23 is part of the specified network.

 Answer: YES.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Explore More IT Terms


Share this term: Facebook X LinkedIn WhatsApp Email

Leave a Reply

Your email address will not be published. Required fields are marked *