Program to find Class, Broadcast and Network addresses

Prerequisite – Introduction and Classful Addressing, Classless Addressing
Given a valid IPv4 address in the form of string. The task is to determine class of given IPv4 address as well as separate Network and Host ID part from it.

IPv4 address:
Every host and router on internet has an IP address which encodes its network number and host number, combination of the two is unique. An IP address does not actually refer to a host but refers to a network interface, so if host is on two networks, it must have two IP addresses.

Default Mask:
An address mask determines which portion of an IP address represents network number and which part represents host number, e.g., IP address, the mask has four octets. If a given bit of the mask is 1, the corresponding bit of the IP address is in network portion and if given bit of mask is 0, the corresponding bit of IP address is in host portion.

CIDR:
Classless Inter Domain Routing uses slash(/) notation to specify the mask with IPv4 address. The address is given as x.y.z.t/n where x.y.z.t is the IP address and n is the number of 1’s in the default mask.

Network address:
First address of a network is network address. It is obtained by ANDing the mask with the IP address (Both in binary form). Another method is to set last 32-n bits of the IP address to 0.

Broadcast address:
Last address of a network is broadcast address. It is obtained by ORing the complement mask with the IP address(Both in binary form). Another method is to set last 32-n bits of the IP address to 1.

Class:
Class of an address is identified by the first byte of address. There are currently five classes A, B, C, D and E. The range of first byte of each class is:

Class A: 0 - 127
Class B: 128 - 191
Class C: 192 - 223
Class D: 224 - 239
Class E: 240 - 255 

Example-1:

CIDR: 192.168.32.1/24(x.y.z.t/n)
IP Address(Binary):  11000000101010000010000000000001
Default Mask(Binary):  11111111111111111111111100000000
Default Mask: 255.255.255.0

32-n=32-24=8  

First Address: Set last 8 bits of IP address to 0
              = 11000000101010000010000000000000
              = 192.168.32.0

Last Address: Set last 8 bits of IP address to 1
              = 11000000101010000010000011111111
              = 192.168.32.255  

Example-2:

CIDR: 205.15.37.39/28(x.y.z.t/n)
IP Address(Binary):  11001101000011110010010100100111
Default Mask(Binary):  11111111111111111111111111110000
Default Mask: 255.255.255.240

32-n=32-28=4  

First Address: Set last 4 bits of IP address to 0
              = 11001101000011110010010100100000
              = 205.15.37.32

Last Address: Set last 4 bits of IP address to 1
              = 11001101000011110010010100101111
              = 205.15.37.47 

Implementation:
The following code uses the concepts mentioned above:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
import java.io.*;
import java.net.*;
import java.lang.Math;
  
class Ip {
      
    // Converts IP address to the binary form
    public static int[] bina(String[] str)
    {
        int re[] = new int[32];
        int a, b, c, d, i, rem;
        a = b = c = d = 1;
        Stack<Integer> st = new Stack<Integer>();
          
        // Separate each number of the IP address
        if (str != null
        {
            a = Integer.parseInt(str[0]);
            b = Integer.parseInt(str[1]);
            c = Integer.parseInt(str[2]);
            d = Integer.parseInt(str[3]);
        }
          
        // convert first number to binary
        for (i = 0; i <= 7; i++) 
        {
            rem = a % 2;
            st.push(rem);
            a = a / 2;
        }
          
        // Obtain First octet
        for (i = 0; i <= 7; i++) {
            re[i] = st.pop();
        }
          
        // convert second number to binary
        for (i = 8; i <= 15; i++) {
            rem = b % 2;
            st.push(rem);
            b = b / 2;
        }
          
        // Obtain Second octet
        for (i = 8; i <= 15; i++) {
            re[i] = st.pop();
        }
          
        // convert Third number to binary
        for (i = 16; i <= 23; i++) {
            rem = c % 2;
            st.push(rem);
            c = c / 2;
        }
          
        // Obtain Third octet
        for (i = 16; i <= 23; i++) {
            re[i] = st.pop();
        }
          
        // convert fourth number to binary
        for (i = 24; i <= 31; i++) {
            rem = d % 2;
            st.push(rem);
            d = d / 2;
        }
          
        // Obtain Fourth octet
        for (i = 24; i <= 31; i++) {
            re[i] = st.pop();
        }
          
        return (re);
    }
      
    // cls returns class of given IP address
    public static char cls(String[] str)
    {
        int a = Integer.parseInt(str[0]);
        if (a >= 0 && a <= 127)
            return ('A');
        else if (a >= 128 && a <= 191)
            return ('B');
        else if (a >= 192 && a <= 223)
            return ('C');
        else if (a >= 224 && a <= 239)
            return ('D');
        else
            return ('E');
    }
      
    // Converts IP address 
    // from binary to decimal form
    public static int[] deci(int[] bi)
    {
          
        int[] arr = new int[4];
        int a, b, c, d, i, j;
        a = b = c = d = 0;
        j = 7;
          
        for (i = 0; i < 8; i++) {
              
            a = a + (int)(Math.pow(2, j)) * bi[i];
            j--;
        }
          
        j = 7;
        for (i = 8; i < 16; i++) {
              
            b = b + bi[i] * (int)(Math.pow(2, j));
            j--;
        }
          
        j = 7;
        for (i = 16; i < 24; i++) {
              
            c = c + bi[i] * (int)(Math.pow(2, j));
            j--;
        }
          
        j = 7;
        for (i = 24; i < 32; i++) {
              
            d = d + bi[i] * (int)(Math.pow(2, j));
            j--;
        }
          
        arr[0] = a;
        arr[1] = b;
        arr[2] = c;
        arr[3] = d;
        return arr;
    }
      
    public static void main(String args[])
    {
  
        int i;
        String[] str = new String[4];
        String ipr = "192.168.1.1/24";
  
        // You can take user input here 
        // instead of using default address
        // Ask user to enter IP address of form(x.y.z.t/n)
        System.out.println("IP address CIDR format is:" + ipr);
          
        // Separate IP address and n
        String[] str1 = ipr.split("/"); 
          
        // IP address
        String tr = str1[0]; 
          
        // Split IP address into 4 subparts x, y, z, t
        str = tr.split("\\."); 
  
        int[] b = new int[32];
  
        System.out.println();
          
        // Convert IP address to binary form
        b = bina(str); 
  
        int n = Integer.parseInt(str1[1]);
        int[] ntwk = new int[32];
        int[] brd = new int[32];
        int t = 32 - n;
          
        // Obtanining network address
        for (i = 0; i <= (31 - t); i++) {
              
            ntwk[i] = b[i];
            brd[i] = b[i];
        }
          
        // Set 32-n bits to 0
        for (i = 31; i > (31 - t); i--) {
              
            ntwk[i] = 0;
        }
          
        // Obtaining Broadcast address 
        // by setting 32-n bits to 1
        for (i = 31; i > (31 - t); i--) {
              
            brd[i] = 1;
        }
          
        System.out.println();
  
        // Obtaining class of Address
        char c = cls(str); 
        System.out.println("Class : " + c);
          
        // Converting network address to decimal
        int[] nt = deci(ntwk); 
          
        // Converting broadcast address to decimal
        int[] br = deci(brd); 
  
        // Printing in dotted decimal format 
        System.out.println("Network Address : " + nt[0
                     + "." + nt[1] + "." + nt[2] + "." + nt[3]); 
          
        // Printing in dotted decimal format
        System.out.println("Broadcast Address : " 
                     + br[0] + "." + br[1] + "." + br[2] + "." + br[3]); 
    }
}

chevron_right


Output:

IP address CIDR format is:192.168.1.1/24


Class : C
Network Address : 192.168.1.0
Broadcast Address : 192.168.1.255 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.