# Codingame Solution: Number of Network Hosts

You need to calculate how many hosts are possible to be in a IPv4 network.

In a network (e.g.: 192.168.0.0/24) you can use all address except the first (192.168.0.0 -> network address) and last (192.168.0.255 -> broadcast address). So there are 254 addresses left to address a host (192.168.0.1 - 192.168.0.254).

In a network with a subnetmask of X are Y addresses possible.

X |Y
1 |2147483648
...
24 |256
25 |128
...
30 |4


Input
First line: N (number of inputs following)

Output
N lines: maximum number of hosts for each network
Constraints
N > 0

Example

Input
1
192.168.0.0/24

Output
254

## Solution

Calculating subnets is fun, as I have shown with the CIDR subnet calculator. Anyway, the CIDR representation counts the number of network bits from left to right in the mask. The number of host bits are the number of bits on the right, or $$32-\text{cidr}$$. The number of hosts is thus $$2^{32-\text{cidr}}$$ minus two for the broadcast and network address. A Python implementation is then

n = int(input())
for i in range(n):
ip, cidr = input().split('/')
print(2**(32 - int(cidr)) - 2)

« Back to problem overview