Java implementation subnet mask conversion to network identification bit

Source: Internet
Author: User
Tags stringbuffer

The subnet mask (subnet mask) is also called the netmask, address mask, and subnet mask, which is used to indicate which bits of an IP address identify the host's subnet and which bits identify the host's bitmask. The subnet mask cannot exist alone, it must be used in conjunction with an IP address. The subnet mask has only one function, that is, to divide an IP address into two parts: network address and host address.

the role of subnet masks

The network address and host number of a device are determined by the binary of the IP address and the subnet mask, which means that the network part and host part of a network are distinguished by the subnet mask. Once the subnet mask is set, the network address and host address are fixed. One of the most notable features of a subnet is that it has a subnet mask. As with IP addresses, the subnet mask is 32-bit in length, or it can be in decimal form. For example, a subnet mask in binary form: 1111 1111, 1111 1111, 1111 1111, 0000 0000, in decimal form: 255.255.255.0.

The presentation method of the network mask the subnet mask usually has the following 2 format representation methods:

1. By dotted decimal notation with the same IP address format

such as: 255.0.0.0 or 255.255.255.128

2. After the IP address plus the "/" symbol and 1-32 of the number, where 1-32 of the number represents the subnet mask in the length of the network identification bit

For example: 192.168.1.1/24 subnet mask can also be expressed as 255.255.255.0

stringbuffer SBF;
        String str;
        String strip= "255.255.255.0";  //Subnet mask
         int inetmask=0,count=0;      //subnet mask abbreviation code
         def iplist=strip.split ('//. ');
         for (int n=0;n<iplist.size (); n++)
            {

SBF = ToBin (Integer.parseint (iplist[n));
Str=sbf.reverse (). toString ();
println (iplist[n]+ "---" +str);

Statistics 2 The number of 1 in the binary string
Count=0
for (int i=0;i<str.length (); i++) {
I=str.indexof (' 1 ', i); Find where the character ' 1 ' appears
if (i==-1) {break;}
count++; Number of statistics characters appear
}
Inetmask+=count;

}
println "Subnet mask abbreviation code:" +INETMASK;


StringBuffer toBin (int x)
{
StringBuffer result=new StringBuffer ();
Result.append (x%2);
x/=2;
while (x>0) {
Result.append (x%2);
x/=2;
}
return result;
}

Related Article

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.