How to judge whether two IP addresses are in the same network segment (super practical)

IP address

The IP address is used to give a number to the computer on the Internet. What everyone sees daily is that every networked PC needs an IP address in order to communicate normally. We can compare "personal computer" to "a telephone", then "IP address" is equivalent to "telephone number", and the router in the Internet is equivalent to the "program-controlled switchboard" of the telecommunications bureau.
The IP address is a 32-bit binary number, usually divided into 4 "8-bit binary numbers" (that is, 4 bytes). The IP address is usually expressed in "dotted decimal notation" in the form of (abcd), where a, b, c, and d are all decimal integers between 0 and 255. Example: The dotted decimal IP address (100.4.5.6) is actually a 32-bit binary number (01100100.00000100.00000101.00000110).
Insert picture description here
Insert picture description here
For example, if a class C address is 192.9.200.13, according to its IP address type, its default subnet mask is: 255.255.255.0, then its network number and host number can be obtained as follows:

Step 1: Convert the IP address 192.9.200.13 to binary 11000000 00001001 11001000 00001101
Step 2: Convert the default subnet mask 255.255.255.0 to binary 11111111 11111111 11111111 00000000
Step 3: Logically perform the above two binary numbers And (AND) operation, the result obtained is the network part. "11000000 00001001 11001000 00001101" and "11111111 11111111 11111111 00000000" are "AND" and get "11000000 00001001 11001000 00000000", which is "192.9.200.0", which is the network number of this IP address, or "network address".
The fourth step is to reverse the binary value of the default subnet mask, and then perform an AND operation with the IP address, and the result is the host part. For example, "00000000 00000000 00000000 11111111 (the value of the subnet mask) is reversed" and "11000000 00001001 11001000 00001101" are ANDed to get "00000000 00000000 00000000 00001101", that is, "0.0.0.13", this is the IP address host Number (can be simplified to "13").

Three of the host bits are occupied by the "network identification number". Because the network identification number should be all "1", the byte segment corresponding to the host number is "11100000". After converted to decimal, it is 224, which is the final subnet mask. If it is a Class C network, the subnet mask is 255.255.255.224; if it is a Class B network, the subnet mask is 255.255.224.0; if it is a Class A network, the subnet mask is 255.224.0.0.

How to judge whether two IP addresses are in the same network segment

To judge whether the two IP addresses are in the same network segment, do the AND operation between their IP addresses and the subnet mask respectively, and the result is a network number. If the network number is the same, it is on the same subnet, otherwise, Not in the same subnet.
Example: Assuming that the subnet mask 255.255.254.0 is selected, and now the above two IP addresses are ANDed with the mask respectively, as shown below:
211.95.165.24 11010011 01011111 10100101 00011000
255.255.254.0 11111111 11111111 111111110 00000000
and the result Is: 11010011 01011111 10100100 00000000
211.95.164.78 11010011 01011111 10100100 01001110
255.255.254.0 11111111 11111111 111111110 00000000
and the result is: 11010011 01011111 10100100 00000000

It can be seen that the results obtained (this result is the network address) are all the same, so it can be judged that the two IP addresses are on the same subnet.

supplement:
Insert picture description here

Guess you like

Origin blog.csdn.net/zhangzhanbin/article/details/112493688