Advertisement
Eternoseeker

subnetting mam

Jun 7th, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | Source Code | 0 0
  1. import java.util.Scanner;
  2.  
  3. class subnetting {
  4.     public static void main(String args[]) {
  5.         Scanner sc = new Scanner(System.in);
  6.         System.out.print("Enter the ip address: ");
  7.         String ip = sc.nextLine();
  8.         String split_ip[] = ip.split("\\."); // SPlit the string after every .
  9.         String split_bip[] = new String[4]; // split binary ip
  10.         String bip = "";
  11.         for (int i = 0; i < 4; i++) {
  12.             split_bip[i] = appendZeros(Integer.toBinaryString(Integer.parseInt(split_ip[i])));
  13.             // "18" => 18 => 10010 => 00010010
  14.             bip += split_bip[i];
  15.         }
  16.         System.out.println("IP in binary is " + bip);
  17.         System.out.print("Enter the number of addresses: ");
  18.         int n = sc.nextInt();
  19.         // Calculation of mask
  20.         int bits = (int) Math.ceil(Math.log(n) / Math.log(2));
  21.         System.out.println("Number of bits required for address =" + bits);
  22.         int mask = 32 - bits;
  23.         System.out.println("The subnet mask is = " + mask);
  24.         // Calculation of first address and last address
  25.         int fbip[] = new int[32];
  26.         for (int i = 0; i < 32; i++)
  27.             fbip[i] = (int) bip.charAt(i) - 48; // convert cahracter 0,1 to integer 0,1
  28.         for (int i = 31; i > 31 - bits; i--)// Get first address by ANDing last nbits with 0
  29.             fbip[i] &= 0;
  30.         String fip[] = { "", "", "", "" };
  31.         for (int i = 0; i < 32; i++)
  32.             fip[i / 8] = new String(fip[i / 8] + fbip[i]);
  33.         System.out.print("First address is = ");
  34.         for (int i = 0; i < 4; i++) {
  35.             System.out.print(Integer.parseInt(fip[i], 2));
  36.             if (i != 3)
  37.                 System.out.print(".");
  38.         }
  39.         System.out.println();
  40.         int lbip[] = new int[32];
  41.         for (int i = 0; i < 32; i++)
  42.             lbip[i] = (int) bip.charAt(i) - 48; // convert cahracter 0,1 to integer 0,1
  43.         for (int i = 31; i > 31 - bits; i--)// Get last address by ORing last nbits with 1
  44.             lbip[i] |= 1;
  45.         String lip[] = { "", "", "", "" };
  46.         for (int i = 0; i < 32; i++)
  47.             lip[i / 8] = new String(lip[i / 8] + lbip[i]);
  48.         System.out.print("Last address is = ");
  49.         for (int i = 0; i < 4; i++) {
  50.             System.out.print(Integer.parseInt(lip[i], 2));
  51.             if (i != 3)
  52.                 System.out.print(".");
  53.         }
  54.         System.out.println();
  55.     }
  56.  
  57.     static String appendZeros(String s) {
  58.         String temp = new String("00000000");
  59.         return temp.substring(s.length()) + s;
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement