确定两个IP地址是否在同一子网上-是从IP地址丢弃前导0还是尾随0? [英] Determining if two IP adresses are on same subnet - is it leading or trailing 0s get dropped from IP address?

查看:100
本文介绍了确定两个IP地址是否在同一子网上-是从IP地址丢弃前导0还是尾随0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果两个IP地址的结果相同,那么我知道两个IP地址是否使用子网掩码进行与"运算,则它们位于同一网络中.如果结果不同,则它们位于不同的网络上.我的问题是,当给定IP地址省略一些0时,多余的0会放在哪里?

I understand if two IP addresses are AND'd with a subnet mask if the result is the same then they are on the same network. If the result is different then they are on different networks. My question is, when given an IP address omitting some 0s where do the extra 0s get placed?

例如,如果子网掩码是255. 128. 0. 0,并且您具有IP 126. 1. 0. 10和IP 126. 127. 0. 1如果您只是盲目地对带有子网掩码的IP进行AND操作,即使它们位于同一网络中,也会得到不同的结果.

For example if the subnet mask is 255 . 128 . 0 . 0 , and you have IP 126 . 1 . 0 . 10 and IP 126 . 127 . 0 . 1 if you just blindly AND the IPs with the subnet mask you get different results even though they are on the same network.

126.1.0.10变成126.100.000.100还是126.001.000.100?

Does 126.1.0.10 become 126.100.000.100 or 126.001.000.100?

阅读回复后进行

255128000000 AND 126001000010 = 1085496376 * 32 * 但是但255128000000 AND 126127000001 = 1085276323 * 84 *
我想念什么吗?

255128000000 AND 126001000010 = 1085496376*32* but 255128000000 AND 126127000001 = 1085276323*84*
Am I missing something?

推荐答案

IPv4地址以四个用点分隔的普通十进制数字(每个数字适合一个字节)书写.

IPv4 addreses are written as four ordinary decimal numbers (each fitting into a byte) separated by dots.

普通十进制数字1是"1",也可以写为001.但是100是一个不同的数字,即一百".

The ordinary decimal number 1 is "one", which can also be written 001. However 100 is a different number, namely "a hundred".

AND操作始终是逐位 AND,因此,要了解它,首先必须了解点分十进制地址和网络掩码如何与原始32位二进制地址相对应:

The AND operation is always a bitwise AND, so to understand it you must first see how the dotted-decimal address and netmask corresponds to a raw binary 32-bit address:

        126   .    127   .     0    .    1
     01111110   01111111   00000000   00000001

        255   .    128   .     0    .    0
AND  11111111   10000000   00000000   00000000
 -----------------------------------------------
     01111110   00000000   00000000   00000000
        126   .      0   .     0    .    0

因此子网掩码为255.128.0.0的126.127.0.1位于子网 126.0.0.0 /9

So 126.127.0.1 with netmask 255.128.0.0 is in subnet 126.0.0.0/9

在软件中,通常将 IPv4地址存储在一个32位变量中-因此126.127.0.1是01111110011111110000000000000001二进制(它也编码为2122252289十进制,除了没人在意 32位IP地址的十进制值),并且仅在需要向用户显示时才将其转换为点分十进制.这种表示法是glglgl所说的乘以256倍.

In software one usually stores IPv4 addresses in a single 32-bit variable -- so 126.127.0.1 is 01111110011111110000000000000001 binary (which also encodes 2122252289 decimal, except that nobody ever cares what the decimal value of a 32-bit IP address is), and it is converted to dotted-decimal only when it needs to be shown to human users. This representation is what glglgl decribes as multiplying by 256 several times.

如果您还具有32位变量(二进制11111111100000000000000000000000000000十进制或4286578688)中的网络掩码,则可以在单个机器指令中对它们进行与"操作,以获取网络地址01111110000000000000000000000000.

If you also have the netmask in a 32-bit variable (11111111100000000000000000000000 binary or 4286578688 decimal), you can AND them in a single machine instruction to get the network address 01111110000000000000000000000000.

这篇关于确定两个IP地址是否在同一子网上-是从IP地址丢弃前导0还是尾随0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆