被赋予IP地址和网络掩码时如何计算的IP范围? [英] How to calculate the IP range when the IP address and the netmask is given?

查看:763
本文介绍了被赋予IP地址和网络掩码时如何计算的IP范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个IP的范围写成一个aaa.bbb.ccc.ddd /掩码( CIDR表示法)我需要来计算第一个和最后包含的IP地址在该范围内使用C#

When a IP-Range is written as aaa.bbb.ccc.ddd/netmask (CIDR Notation) I need to calculate the first and the last included ip address in this range with C#.

示例:

输入: 192.168.0.1/25

Input: 192.168.0.1/25

结果: 192.168.0.1 - 192.168.0.126

Result: 192.168.0.1 - 192.168.0.126

推荐答案

我的好朋友亚历山德罗有< A HREF =htt​​p://blog.typps.com/2007/10/bitwise-operators-in-c-or-xor-and-not.html>关于C#位运算符好贴,则应该了解它,所以你知道该怎么做。

my good friend Alessandro have a nice post regarding bit operators in C#, you should read about it so you know what to do.

这是pretty容易。如果你打破给你二进制的IP,网络地址是IP地址,所有的主机位(0的子网掩码)为0,而最后一个地址,广播地址,是所有主机位是1。

It's pretty easy. If you break down the IP given to you to binary, the network address is the ip address where all of the host bits (the 0's in the subnet mask) are 0,and the last address, the broadcast address, is where all the host bits are 1.

例如:

ip 192.168.33.72 mask 255.255.255.192

11111111.11111111.11111111.11000000 (subnet mask)
11000000.10101000.00100001.01001000 (ip address)

粗体部分是主机位(其余的都是网络位)。如果你把所有的主机位为0的IP,你得到的第一个可能的IP:

The bolded parts is the HOST bits (the rest are network bits). If you turn all the host bits to 0 on the IP, you get the first possible IP:

11000000.10101000.00100001.01000000 (192.168.33.64)

如果你把所有的主机位为1的,那么你得到的最后可能的IP(又名广播地址):

If you turn all the host bits to 1's, then you get the last possible IP (aka the broadcast address):

11000000.10101000.00100001.01111111 (192.168.33.127)

因此​​,对于我的例子:

So for my example:

the network is "192.168.33.64/26":
Network address: 192.168.33.64
First usable: 192.168.33.65 (you can use the network address, but generally this is considered bad practice)
Last useable: 192.168.33.126
Broadcast address: 192.168.33.127

这篇关于被赋予IP地址和网络掩码时如何计算的IP范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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