Ip总量计算工具
public class IPTotalUtil {
public static void main(String[] args) {
Integer ipTotal = getIpTotal("192.168.0.1/20");
System.out.println(ipTotal);
}
public static Integer getIpTotal(String cidr){
String[] split = cidr.split("/");
Integer num = Integer.valueOf(split[1]);
return powerTwo(32 - num);
}
public static int powerTwo(int N) {
StringBuilder stringBuilder = new StringBuilder("");
long num[] = new long[2];
num[1] = 1;
if (N > 62) {
num[0] = 1;
num[0] = num[0] << (N - 62);
num[1] = num[1] << 62;
String s = String.valueOf(num[1]);
int size = 30, i = 0, j = 0;
long n[] = new long[size + 1];
for (i = s.length() - 1; i >= 0; --i) {
n[j++] = (long) (num[0] * (s.charAt(i) - '0'));
}
for (i = 0; i < size; ++i) {
while (n[i] > 9) {
n[i + 1] += n[i] / 10;
n[i] %= 10;
}
}
boolean bl = false;
for (i = size; i >= 0; --i) {
if (n[i] != 0 || bl) {
stringBuilder.append(n[i]);
bl = true;
}
}
} else {
num[1] = num[1] << N;
stringBuilder.append(String.valueOf(num[1]));
}
return Integer.valueOf(stringBuilder.toString());
}
}