如何从IP地址和子网掩码中确定网络和广播地址?

时间:2015-03-13 14:30:51

标签: networking network-programming ip broadcast subnet

例如:

  • IP地址:130.45.34.36
  • 面具:255.255.240.0

什么是网络ID /子网地址,和 广播地址?

5 个答案:

答案 0 :(得分:47)

让我们用二进制写:

130.45.34.36 = 10000010.00101101.00100010.00100100

255.255.240.0 = 11111111.11111111.11110000.00000000

两者之间的按位 AND 将为我们提供网络地址:

10000010.00101101.00100010.00100100   (ip address)
AND
11111111.11111111.11110000.00000000   (subnet mask)
=
10000010.00101101.00100000.00000000 = 130.45.32.0 (the resulting network address)

网络地址和反向子网掩码之间的按位 OR 将为我们提供广播地址:

10000010.00101101.00100000.00000000   (netadress)
OR
00000000.00000000.00001111.11111111   (inverted subnet mask)
=
10000010.00101101.00101111.11111111 = 130.45.47.255 (broadcast address)

答案 1 :(得分:2)

Flask==0.12.1
matplotlib==1.4.3
six
pandas
statsmodels
arch
sklearn
mpld3

答案 2 :(得分:1)

获取netwotk地址后广播地址计算的另一个捷径是:

  1. 计算主机总数(在这种情况下为2 ^ 12 = 4096)

  2. 除以256(在这种情况下为16)并在*对应的八位字节中添加结果-1(在本例中为15)(在这种情况下为第二个八位字节,即 32 + 15 = 47)并制作其他八位字节255

  3. *我们可以通过查看主机的no来获得相应的八位字节。例如,如果主机的数量大于256,那么我们必须将它添加到网络地址的第二个八位字节,依此类推

答案 3 :(得分:0)

基于麦芽答案的代码示例:

const
    ipadr = '130.45.34.36',
    subnet = '255.255.240.0',
    ipadrs = ipadr.split('.'),
    subnets = subnet.split('.');

let networks = [],
    broadcasts = [];

for (let i in ipadrs) {
    networks[i] = ipadrs[i] & subnets[i];
}

console.log('netaddress: ', networks.join('.')) // netaddress:  130.45.32.0

for (let i in networks) {
    broadcasts[i] = networks[i] | ~subnets[i] + 256;
}

console.log('broadcast address: ', broadcasts.join('.')) // broadcast address:  130.45.47.255

答案 4 :(得分:0)

打字稿版本

import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home.component';

const routes: Routes = [
  {
    path: '',
    component: HomeComponent
  }

];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class HomeRoutingModule {}