Back
Close

IP mask calculating

Statement

 Goal

Given an IP address and a mask in CIDR format you must output the network address and the broadcast address.

The IP address is the address of a computer on a network. In an IP address there are two parts, the network part and the machine part. We can separate the two parts with the mask.

The mask is, in binary, a continuous part of 1s flowing by a continuous part of 0s. It can't be a mixture of 1 and 0.

To know the network address of an IP we proceed like this:
For the entry 192.168.0.15/24, the machine IP is 192.168.0.15 and the mask part is 24.

The mask part means the first 24 bits in the mask are set at 1 and the last ones are set to 0:
The mask is 11111111.11111111.11111111.00000000
In integers that corresponds to 255.255.255.0

To know the network address we take all bits with a 1 in the mask:
192.168.0.15 is in binary
11000000.10101000.00000000.00001111
11111111.11111111.11111111.00000000 -> is the mask in binary
Now the network part of the IP address is:
11000000.10101000.00000000
After, we place all 0 we need to get a total of 32 digits
The network address is now
10000000.10101000.00000000.00000000
In integer that means
192.168.0.0

The broadcast address is the network address with all bits in machine part set to 1.

If we take the same example we got this
192.168.0.15 is in binary
11000000.10101000.00000000.00001111
11111111.11111111.11111111.00000000 -> is the mask in binary
The broadcast address is:
11000000.10101000.00000000.11111111 -> in integer that means 192.168.0.255.
Input
An IP address and a mask in CIDR format.
Output
Line 1: The network address
Line 2: The broadcast address
Constraints
All entries can be calculated.
Example
Input
192.168.0.5/24
Output
192.168.0.0
192.168.0.255

Tags
Loops

Difficulty
Medium

Test cases
Your ip at home Test
Input
192.168.0.5/24
Output
192.168.0.0 192.168.0.255

Your ip at home Validator
Input
192.168.1.5/24
Output
192.168.1.0 192.168.1.255

Your ip at school Test
Input
10.0.1.34/24
Output
10.0.1.0 10.0.1.255

Your ip at school Validator
Input
10.34.1.87/24
Output
10.34.1.0 10.34.1.255

Not a real network mask Test
Input
77.45.23.98/32
Output
77.45.23.98 77.45.23.98

Not a real network mask Validator
Input
0.0.0.0/32
Output
0.0.0.0 0.0.0.0

Little mask (not a real network mask too) Test
Input
78.23.88.1/0
Output
0.0.0.0 255.255.255.255

Little mask (not a real network mask too) Validator
Input
234.98.23.4/0
Output
0.0.0.0 255.255.255.255

255.255.240.0 Test
Input
234.67.0.5/20
Output
234.67.0.0 234.67.15.255

255.255.240.0 Validator
Input
245.69.32.45/20
Output
245.69.32.0 245.69.47.255

254.0.0.0 Test
Input
77.65.155.7/7
Output
76.0.0.0 77.255.255.255

254.0.0.0 Validator
Input
79.3.5.2/7
Output
78.0.0.0 79.255.255.255

A strange IP Test
Input
09.45.000.2/18
Output
9.45.0.0 9.45.63.255

A strange IP Validator
Input
98.045.000.9/18
Output
98.45.0.0 98.45.63.255

42 Test
Input
42.42.42.42/10
Output
42.0.0.0 42.63.255.255

37 Validator
Input
37.37.37.37/10
Output
37.0.0.0 37.63.255.255

Solution language

Solution

Stub generator input