Subnet mask, mask length relationship

Source: Internet
Author: User

/***************************************************************************** * 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.