以下程序通过输入类似于192.168.16.24/27的命令来计算该IP所属的网络号,仅供娱乐。
 
如有错误谢谢指出

package cn.ifiv5.IP;
import java.io.*;
 
/**
 *
 * @author fanghao 
*/
public class Main {
    public static void main(String[] args) {
        new Main().go();
    }
 public void go() {
        String IP = null;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try{
            IP = br.readLine();
        }catch(IOException ex){
            ex.printStackTrace();
        }
         
        IP = IP.replace(" ", "");
        String []temp = IP.split("/");
        String []sIP = temp[0].split("\\.");
        int Mask = Integer.parseInt(temp[1]);
        int []ip = new int[4];
     
        for(int i=0;i        {
         ip[i] = Integer.parseInt(sIP[i]);
        }
         
        int b=0;
        int a = Mask%8;
         
        b=Mask/8;
         
        StringBuffer sb = new StringBuffer();
         
        for(int i=0;i         sb.append('1');
        for(int i=0;i<8-a;i++)
         sb.append('0');
         
        int end =Integer.parseInt(sb.toString(),2);
       
        ip[b] = ip[b]&end;
        for(int i=b+1;i<4;i++)
         ip[i] = 0;
         
        for(int i=0;i<4;i++)
        {
         System.out.print(ip[i]);
         if(i!=3)
          System.out.print(".");
        }
         
         
    }
}