/***************************************************************************** * Subnet mask, mask length relationship * Statement: * When we operate the Linux system, we often see that we can use the subnet mask, mask length to represent the mask, * for me, has been curious is the subnet mask between 1 can clip a 0, if the clip 0, the mask length * bad calculation. * 2016-5-5 Shenzhen Nanshan Ping Shan village Zengjianfeng ****************************************************** **********************/First, reference article:1. Netmask v. Address Prefix Length http://www.gadgetwiz.com/network/netmask.html 2. Android.net.NetworkUtils http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/net/ Networkutils.java#networkutils.netmaskinttoprefixlength%28int%29Second, Reason (reference article explanation): Netmasks is only counted asThe number of zeros fromthe right; There is no zerosinchThe middle of a netmask. After all, a netmask like255.255.255.250(...11111010) wouldn'T make much sense since it would refer to a range including only odd IP addresses. It wouldn'T is a range at all!Consequently, there is only a few valid network masks. Each specify a number half the size of the prior netmask. Summary: From the reference article, the subnet mask in front of 1 is not allowed to exist 0, of course, if there is 0, then the calculation of the length of the mask can not be calculated. Four, subnet mask, mask length conversion: android.net.NetworkUtils1. Convert a IPv4 netmask integer to a prefix length2. Parameters:netmask asAn integerinchNetworkbyteOrder3. Returns:the Network Prefix length4. Code: Public Static intMore ... netmaskinttoprefixlength (intnetmask) { returnInteger.bitcount (netmask); }
Subnet mask, mask length relationship