How to determine whether two IP addresses are in the same network segment? What is a subnet mask?

How to determine whether two IP addresses are in the same network segment? What is a subnet mask?

We have read many articles before about IP addresses, and everyone has a certain understanding of them. However, some friends still have some questions about subnet masks and IP address segments. So today we will explain this together.

1. What is a subnet mask?

Before understanding the network segment of the IP address, let's first understand the subnet mask. Many friends who don't know much about the network are confused about the subnet mask. They don't know what it is used for?

The subnet mask cannot exist alone, it must be used in conjunction with the IP address. The subnet mask has only one function, which is to divide an IP address into two parts: the network address and the host address.

In simple terms, it is used to divide subnets and distinguish which IPs are in the same network segment and which are not.

For example, two people are both named Zhang San, but one Zhang San is from Zhangjia Village and the other Zhang San is from Zhang Village. How can we distinguish which village the two Zhang Sans belong to? The village chief must be asked to distinguish them so that they can accurately lead each Zhang San back to the village. The subnet mask is equivalent to the village chief, and it is used to distinguish which network segment the IP address belongs to.

In actual projects, we often encounter such IP addresses.

IP address: 192.168.1.1 Subnet mask: 255.255.255.0

IP address: 192.168.1.2 Subnet mask: 255.255.255.0

We can directly judge that they are IP addresses belonging to the same network segment.

So what about the following?

IP address: 192.168.1.1 Subnet mask: 255.255.255.0

IP address: 192.168.1.2 Subnet mask: 255.255.0.0

Although these two IP addresses are similar without looking at the mask, they are not in the same network segment.

This can be determined from the subnet mask.

192.168.1.1 255.255.255.0 belongs to the 192.168.1.0 network segment.

And 192.168.1.2 255.255.0.0 belongs to the 192.168.0.0 network segment.

2. How to determine the IP address segment based on the mask

Above we have given an example to understand the role of the subnet mask. Next, let’s learn how to determine the subnet mask and judge the network segment of the IP address.

Usually we use the following examples when dividing VLANs:

Example 1:

Create vlan1: IP address: 192.168.1.1 Subnet mask: 255.255.255.0

Create vlan2: IP address: 192.168.2.1 Subnet mask: 255.255.255.0

So are they in the same network segment? Friends who usually configure more IP addresses can intuitively judge that they do not belong to the same network segment, so how to calculate? To determine whether two IP addresses are in the same network segment, just perform an AND operation on the IP address and the subnet mask. If the results are the same, the two IP addresses are in the same subnet.

Detailed calculation

Convert ip address 192.168.1.1 to binary

11000000 10101000 00000001 00000001

Convert the subnet mask 255.255.255.0 to binary

11111111.11111111.11111111.00000000

Then perform an AND operation on the two:

11000000 10101000 00000001 00000001

11111111.11111111.11111111.00000000

Then we get:

11000000 10101000 00000001 00000000

Converted to network number: 192.168.1.0

Convert ip address 192.168.2.1 to binary

11000000 10101000 00000010 00000001

Convert the subnet mask 255.255.255.0 to binary

11111111.11111111.11111111.00000000

Then perform an AND operation on the two:

11000000 10101000 00000010 00000001

11111111.11111111.11111111.00000000

Then we get:

11000000 10101000 00000010 00000000

Converted to network number: 192.168.2.0

Obviously, the two results are different, one is the network segment 192.168.1.0, and the other is the network segment 192.168.2.0, so they are not the same network segment.

Example 2

IP address 1: 192.168.1.1 Subnet mask: 255.255.252.0

IP address 2: 192.168.2.1 Subnet mask: 255.255.252.0

Obviously, our IP address is the same as that in Example 1 above, but the subnet mask is different. If we don't look at the subnet mask, we might first determine that they are not in the same network segment.

The biggest change here from Example 1 above is that the subnet mask has changed from 255.255.255.0 to 255.255.252.0. Let's see if they belong to the same network segment.

Detailed calculation

Convert ip address 192.168.1.1 to binary

11000000 10101000 00000001 00000001

Convert the subnet mask 255.255.252.0 to binary

11111111.11111111.11111100.00000000

Then perform an AND operation on the two:

11000000 10101000 00000001 00000001

11111111.11111111.11111100.00000000

Then we get:

11000000 10101000 00000000 00000000

Converted to network number: 192.168.0.0

Convert ip address 192.168.2.1 to binary

11000000 10101000 00000010 00000001

Convert the subnet mask 255.255.252.0 to binary

11111111.11111111.11111100.00000000

Then perform an AND operation on the two:

11000000 10101000 00000010 00000001

11111111.11111111.11111100.00000000

Then we get:

11000000 10101000 00000000 00000000

Converted to network number: 192.168.0.0

Obviously, the results of the two are the same, both belong to the 192.168.0.0 network segment, so they belong to the same network segment.

Of course, if you are familiar with subnets, you do not need to go through these calculations. Most network segments can be determined whether they belong to network segment 1 by analyzing the subnet mask and IP address, but the prerequisite is that you need to be familiar with the principles and calculation methods of network segments.

3. How to use IP address subnet mask

Determine the IP address subnet mask

In most cases, as long as there are no more than 254 hosts in our network, the subnet mask can be set to 255.255.255.0. So what if there are more than 254 hosts?

You can divide VLANs or set them up as a large network segment. We won't talk about dividing VLANs. There is an example above. There is a difficulty in setting up a large network segment. How to determine the subnet mask in this large network segment?

For example

Assume that there are 600 points in the monitoring network, and we do not want to divide it into VLANs (most of the actual projects will divide VLANs). Assume that we only want to use a large network segment to assign IP addresses to these 600 points. How to set the IP address and how to determine the subnet mask?

Analysis: First, we know that 600 points can be allocated using 3 254 IP address segments.

Can be used

IP segment 1: 192.168.0.1-192.168.0.254

IP segment 2: 192.168.1.1-192.168.1.254

IP segment three: 192.168.2.1-192.168.2.254

Each network segment has 254 IP addresses, which is enough for 600 locations.

So the question is, if we want to make these three IP segments in the same network segment, what is the common subnet mask of this large network segment?

Let's analyze it:

Convert ip address 192.168.0.1 to binary

11000000 10101000 00000000 00000001

Convert ip address 192.168.1.1 to binary

11000000 10101000 00000001 00000001

Convert ip address 192.168.2.1 to binary

11000000 10101000 00000010 00000001

After converting to binary, we can see that the first 22 bits of the three IP segments remain unchanged, so they can be expressed as:

IP segment 1: 192.168.0.1/22

IP segment 2: 192.168.1.1/22

IP segment 3: 192.168.2.1/22

I believe everyone has seen this type of IP address in the form of 192.168.1.x/22, which already tells the subnet mask.

That is to say, the first 22 digits of their common subnet mask binary are all 1.

11111111 11111111 11111100 00000000

Converted into decimal, that is 255.255.252.0, so their common subnet mask is 255.255.252.0.

Of course, in actual projects, weak current people who have more contact with the network do not need to go through a series of conversions, and can directly estimate the subnet mask of this large network segment through simple judgment. Therefore, today I will explain the principle to facilitate everyone's understanding, so all conversions are calculated.

<<:  This move can save tens of billions of yuan in 5G construction!

>>:  Four Key Words for Successful Large-Scale Deployment of IPv6

Recommend

With this subnet division summary, I know all about subnet mask design~

1. Subnet Division Subnet division is actually th...

Home broadband can also be wirelessly accessed. What are the benefits for IoT?

People are looking forward to a lower-priced broa...

What is Power over Ethernet (PoE) and what are its benefits?

Power over Ethernet (PoE) is a technology that tr...

ArticHost: $3.19/month KVM-2GB/60GB/100M unlimited traffic/Chicago data center

According to information from LEB, ArticHost is a...

...

Research shows: 80% of enterprises will strengthen 5G and edge plans

Edge and 5G have become leading areas of investme...

How Wi-Fi 6, WWAN and 5G make fully wireless office possible

For use cases, fully wireless connectivity for of...

The IPv6 era is coming, and every grain of sand in the world can have an IP

When it comes to IPv6, I think most people think ...

Four trends to watch out for in 2018

Even with all the coffee or energy drinks in the ...

In the 5G era, how can telecom operators lead the future through IoT services?

Major global telecom operators have been explorin...

Will the difficulties faced by the communications industry continue in 2019?

Even though this topic is a bit heavy, as researc...

Temperature compensation: What is compensated is not actually the temperature?

How do batteries work? In communication power sup...

Technical details: Basic principles of IPSec VPN

IPSec VPN is a technology with a very high click ...