获取子网络地址范围
/** 来 自 时 代 Java - nowjava.com **/ //package com.nowjava; public class Main { /** * * @param hostAddress * ,ex: 192.168.1.222 * @param networkPrefixLength * ,ex: 24 * @return */ public static byte[][] getSubNetworkAddressRange(String hostAddress, short networkPrefixLength) { byte[] addressBytes = getBytesOfHostAddress(hostAddress); byte[][] addressRange = new byte[2][]; addressRange[0] = new byte[addressBytes.length]; addressRange[1] = new byte[addressBytes.length]; final int addressBitLength = addressBytes.length * 8; for (int i = 0; i < addressBytes.length; i++) { if ((i + 1) * 8 + networkPrefixLength <= addressBitLength) {/*n o w j a v a . c o m - 时 代 Java 提 供*/ // host addressRange[0][i] = 0; addressRange[1][i] = (byte) 0xff; } else if (i * 8 + networkPrefixLength < addressBitLength) { // host and network addressRange[0][i] = addressRange[1][i] = addressBytes[i]; byte bits = (byte) 0xff; int moveBitCount = 8 - networkPrefixLength % 8; bits = (byte) (bits >>> moveBitCount << moveBitCount); addressRange[0][i] &= bits; addressRange[1][i] |= (~bits); } else { // network addressRange[0][i] = addressRange[1][i] = addressBytes[i]; } } if (addressRange[0][0] == 0) { addressRange[0][0] = 1; } if (addressRange[1][0] == (byte) 0xff) { addressRange[1][0]--; } return addressRange; } /** * * @param hostAddress * ex:192.168.1.222 * @return */ public static byte[] getBytesOfHostAddress(String hostAddress) { String[] hostAddressParts = hostAddress.split("\\."); byte[] bytes = new byte[hostAddressParts.length]; for (int i = 0; i < hostAddressParts.