# Determine the minimum address range that contains some given IPs

I recently started to learn how IPv4 works. I know how to divide a given address range in smaller ones and set up a network in that way (at least in Netsim K).

But I wanted to do a bit of something else. Let's say I have 2 IP addresses, P1 and P2 and a netmask M. How can I determine the smallest range that contains both P1 and P2. I can start by doing P1 & M to determine P1's network and P2 & M for P2's. If these networks are the same I should just try to divide that range into smaller ones. But what if they are different.

Let's say I have the netmask: 255.255.192.0 (/18) and the IPs: 193.231.20.17 and 193.230.14.208. This gives me: 193.231.0.0 and 193.230.0.0 for the network addresses. Doing IP or not netmask gives: 193.231.63.255 and 193.230.63.255 as broadcast addresses. What should I do now? It seems like I did something wrong. But assuming I didn't, should I try to merge 193.230.0.0 / 18 with the above range and so on until I have those 2 IPs in the same network?

EDIT: after the answer, here is the way I did this for the above example (just in case someone else stumbles upon this in the future).

193.230.014.208 and 193.231.020.017 have 15 bits in common. In base 2: 193.11100110.don't care (also, don't care about 193) 193.11100111. This means that the network these 2 share has a /15 netmask. Doing a logical and between any of the addresses and this new netmask will give us the desired network.