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: (/18) and the IPs: and This gives me: and for the network addresses. Doing IP or not netmask gives: and 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 / 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). and 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.

1 Answer 1


Remove a bit from the mask (expand the range) and try again to see if you get the same network for both. Keep doing that until the networks ANDed with the mask are the same.

  • So this was the right way. I thought I was doing something obviously wrong. Thanks.
    – icebp
    Commented Jun 14, 2015 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.