获取苹果酒基IP
/**N o w J a v a . c o m**/ import java.util.ArrayList; import java.util.regex.Pattern; public class Main{ public static void main(String[] argv) throws Exception{ long ip = 2; int cidr = 2; System.out.println(getCiderBaseIP(ip,cidr)); } public static long getCiderBaseIP(final long ip, final int cidr) throws InvalidIPAddressException { long netmask = getSubnetMaskNumeric(cidr);// 来 自 时代Java公众号 // get base network ip for this ip/netmask combo long baseIP = ip & netmask; return baseIP; } public static long getSubnetMaskNumeric(final int cidr) throws InvalidIPAddressException { if (cidr > 32 || cidr < 0) throw new InvalidIPAddressException( "CIDR can not be greater than 32"); // starting /24 netmask, in decimal (255.255.255.0) long netmask = 4294967040L; // calculating and correcting netmask if (cidr > 24) { for (long i = cidr; i > 24; i--) { netmask += (long) (java.lang.Math.pow(2, (32 - i)));