如何判定两个IP地址是否在同一个网段?什么是子网掩码?( 二 )


portant; overflow-wrap: break-word !important; color: rgb(84, 84, 84); font-family: 微软雅黑, 宋体, 黑体, Arial, Helvetica, sans-serif; font-size: 15px; letter-spacing: 2px; text-indent: 30px;">而192.168.1.2 255.255.0.0是属于192.168.0.0网段 。
portant; overflow-wrap: break-word !important; font-size: 15px;">二、如何根据掩码来确定ip地址网段
portant; overflow-wrap: break-word !important; color: rgb(84, 84, 84); font-family: 微软雅黑, 宋体, 黑体, Arial, Helvetica, sans-serif; font-size: 15px; letter-spacing: 2px; text-indent: 30px;">上面我们已经举例了解了子网掩码的作用,接下来我们再来了解如何确定子网掩码和判定ip地址的网段 。
portant; overflow-wrap: break-word !important; color: rgb(84, 84, 84); font-family: 微软雅黑, 宋体, 黑体, Arial, Helvetica, sans-serif; font-size: 15px; letter-spacing: 2px; text-indent: 30px;">通常我们在划分vlan的时候会使用以下例子:
portant; overflow-wrap: break-word !important; color: rgb(217, 33, 66);">portant; overflow-wrap: break-word !important; font-family: 微软雅黑, 宋体, 黑体, Arial, Helvetica, sans-serif; font-size: 15px; letter-spacing: 2px; text-indent: 30px;">例1:
portant; overflow-wrap: break-word !important; color: rgb(84, 84, 84); font-family: 微软雅黑, 宋体, 黑体, Arial, Helvetica, sans-serif; font-size: 15px; letter-spacing: 2px; text-indent: 30px;">创建vlan1:ip地址:192.168.1.1 子网掩码:255.255.255.0
portant; overflow-wrap: break-word !important; color: rgb(84, 84, 84); font-family: 微软雅黑, 宋体, 黑体, Arial, Helvetica, sans-serif; font-size: 15px; letter-spacing: 2px; text-indent: 30px;">创建vlan2: ip地址:192.168.2.1 子网掩码:255.255.255.0
portant; overflow-wrap: break-word !important; color: rgb(84, 84, 84); font-family: 微软雅黑, 宋体, 黑体, Arial, Helvetica, sans-serif; font-size: 15px; letter-spacing: 2px; text-indent: 30px;">那么他们是不是在同一个网段呢?平时配置ip地址较多的朋友,可以直观的判定,他们并不是属于同一个网段,那么如何计算呢?portant; overflow-wrap: break-word !important; font-family: Microsoft Yahei, 微软雅黑, arial, 宋体, sans-serif; font-size: 16px;">要想判定两个ip地址是不是在同一个网段,只需将ip地址与子网掩码做与运算,假如得出的结果一样,则这两个ip地址是同一个子网当中 。
portant; overflow-wrap: break-word !important; font-size: 15px;">具体计算
portant; overflow-wrap: break-word !important; font-size: 15px;">将ip地址192.168.1.1转换为二进制
portant; overflow-wrap: break-word !important; font-size: 15px;">11000000 10101000 00000001 00000001
portant; overflow-wrap: break-word !important; font-size: 15px;">将子网掩码255.255.255.0转换成二进制
portant; overflow-wrap: break-word !important; font-size: 15px;">11111111.11111111.11111111.00000000
portant; overflow-wrap: break-word !important; font-size: 15px;">然后将两者相“与(and)运算:
portant; overflow-wrap: break-word !important; font-size: 15px;">11000000 10101000 00000001 00000001
portant; overflow-wrap: break-word !important; font-size: 15px;">11111111.11111111.11111111.00000000
portant; overflow-wrap: break-word !important; font-size: 15px;">然后得到:
portant; overflow-wrap: break-word !important; font-size: 15px;">11000000 10101000 00000001 00000000
portant; overflow-wrap: break-word !important; font-size: 15px;">转换成网络号就是:192.168.1.0
portant; overflow-wrap: break-word !important; font-size: 15px;">将ip地址192.168.2.1转换为二进制
portant; overflow-wrap: break-word !important; font-size: 15px;">11000000 10101000 00000010 00000001
portant; overflow-wrap: break-word !important; font-size: 15px;">将子网掩码255.255.255.0转换成二进制
portant; overflow-wrap: break-word !important; font-size: 15px;">11111111.11111111.11111111.00000000
portant; overflow-wrap: break-word !important; font-size: 15px;">然后将两者相“与(and)运算:
portant; overflow-wrap: break-word !important; font-size: 15px;">11000000 10101000 00000010 00000001
portant; overflow-wrap: break-word !important; font-size: 15px;">11111111.11111111.11111111.00000000
portant; overflow-wrap: break-word !important; font-size: 15px;">然后得到:
portant; overflow-wrap: break-word !important; font-size: 15px;">11000000 10101000 00000010 00000000
portant; overflow-wrap: break-word !important; font-size: 15px;">转换成网络号就是:192.168.2.0
portant; overflow-wrap: break-word !important; font-size: 15px;">很明显,二者的结果是不一样的,一个是网段portant; overflow-wrap: break-word !important; letter-spacing: 0.544px;">192.168.1.0,一个是网段portant; overflow-wrap: break-word !important; letter-spacing: 0.544px;">192.168.2.0,所以不是一个网段 。