[Reprint] Calculate ip based on subnet mask and gateway

1. What is a subnet mask?

Before understanding the network segment of the ip address, let’s understand the subnet mask first. Many friends who don’t know much about the network are a little 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 layman’s terms, it is used to divide subnets and distinguish those IPs that are in the same network segment and those that are not. 【Router Product Encyclopedia】

For example, two people are called Zhang San, but one Zhang San belongs to Zhangjia Village and the other Zhang San belongs to Zhang Village, so how to distinguish which village these two Zhang San belong to? Let the village head distinguish, so that they can accurately bring Zhang San back to the village, then the subnet mask is equivalent to the village head, and it is used to distinguish which network segment the ip address belongs to.

In actual projects, we usually 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 the two ip addresses are similar without looking at the mask, they are not in the same network segment.

This can be judged 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 network segment according to the mask

Above we have given an example to understand the role of the subnet mask, and then we will learn how to determine the subnet mask and determine 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 on 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 it? To determine whether the two ip addresses are in the same network segment, you only need to perform an AND operation on the ip address and the subnet mask. If the result is 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 subnet mask 255.255.255.0 to binary

11111111.11111111.11111111.00000000

Then "and" the two:

11000000 10101000 00000001 00000001

11111111.11111111.11111111.00000000

and then get:

11000000 10101000 00000001 00000000

Converted to a network number is: 192.168.1.0

Convert ip address 192.168.2.1 to binary

11000000 10101000 00000010 00000001

Convert subnet mask 255.255.255.0 to binary

11111111.11111111.11111111.00000000

Then "and" the two:

11000000 10101000 00000010 00000001

11111111.11111111.11111111.00000000

and then get:

11000000 10101000 00000010 00000000

Converted to a network number is: 192.168.2.0

Obviously, the results of the two 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 the above example 1, but the subnet mask is different. If you don’t look at the subnet mask, you may first judge that they are not the same network segment.

The only change here and in Example 1 above is that the subnet mask has been 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 subnet mask 255.255.252.0 to binary

11111111.11111111.11111100.00000000

Then "and" the two:

11000000 10101000 00000001 00000001

11111111.11111111.11111100.00000000

and then get:

11000000 10101000 00000000 00000000

Converted to a network number is: 192.168.0.0

Convert ip address 192.168.2.1 to binary

11000000 10101000 00000010 00000001

Convert subnet mask 255.255.252.0 to binary

11111111.11111111.11111100.00000000

Then "AND" the two:

11000000 10101000 00000010 00000001

11111111.11111111.11111100.00000000

and then get:

11000000 10101000 00000000 00000000

Converted to a network number is: 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 the subnet, you don’t need to go through these calculations. Most network segments can be judged whether they belong to the first network segment by analyzing the subnet mask and ip address, but the premise is that you need to be familiar with the network segment. The principle of segment and the method of operation.

3. How to calculate the subnet mask according to the ip address

determine 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?

It can be divided into vlans, and it can also be set into a large network segment. We will not talk about the division of vlans. There are examples 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 don’t want to divide it into vlans (most of the actual projects will divide vlans), assuming that we only want to use a large network segment to assign ip addresses to these 600 points, how How to confirm the subnet mask when setting the ip address?

Analysis: First of all, we know 600 points, which can be allocated using three 254 ip address segments.

can use

IP segment one: 192.168.0.1–192.168.0.254

IP segment two: 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, fully enough for 600 points.

So here comes the question, if these three ip segments are to be in the same network segment, what is the common subnet mask of this large network segment?

Let’s analyze:

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

Converted into binary, it can be seen that the first 22 bits of the binary of the three ip segments are unchanged, so they can be expressed as:

IP segment one: 192.168.0.1/22

IP segment two: 192.168.1.1/22

IP segment three: 192.168.2.1/22

This kind of ip address in the form of 192.168.2.x/22 is believed to be seen by everyone, that is, the subnet mask has been told.

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

11111111 11111111 11111100 00000000

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

Of course, in actual projects, 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 judgments. Therefore, today I will talk about the principle for everyone to understand. So all conversions are calculated.

Original address: https://blog.csdn.net/qq_40309341/article/details/109118471