• Difficulty Level : Medium
• Last Updated : 13 Mar, 2023

An IP address is an important concept in computers and networking. IP address helps each packet which uses routing tables to determine the best route for the packet to take to reach its destination. That makes the Class of an IP address an important concept so let us check how to find a class from a Binary IP address.

Representation of addresses in classful addressing can specifically be in 2 types as follows:

1. Binary notation
2. Dotted-decimal notation

Recognizing classes

We can find the classes of an address when the address is given either in binary or dotted-decimal notation. In binary notation, the first few bits can immediately tell us the class of the address; in dotted-decimal notation, the value of the first byte can give the class of an address as shown below figure.

Different classes of Internet Protocol

Steps to find the class is defined below:

1. As we know from the above figure the first octet determines the class in classful addressing, we will consider the first 8 bits.
2. check the first bit if it is 0 then class A.
3. If not check the second bit if it is 0 then class B.
4. If the first bit is 1 & the second bit is 1 and the third bit is 0 then the class is C.
5. If all the first 3 bits are 1 and the fourth bit is o then class D.
6. If all the first bits are 1 then class E.

Example:

C

 `// C Program to find class from``// Binary IP  address classful addressing.``#include ``#include ` `// Driver Code``int` `main()``{``    ``// IP declared IPv4``    ``// 11100110.00110100.01101010.11010100``    ``int` `s[32] = { 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1,``                  ``1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0,``                  ``1, 0, 1, 1, 0, 1, 0, 1, 0, 0 };` `    ``// Checking on all characters``    ``for` `(``int` `i = 0; i < 32; i++) {``        ``if` `(!(s[i] == 0 || s[i] == 1)) {``            ``printf``(``"Invalid"``);``            ``break``;``        ``}` `        ``if` `(i != 0 && i % 8 == 0)``            ``printf``(``" "``);` `        ``printf``(``"%d"``, s[i]);``    ``}` `    ``// Condition for Class A``    ``if` `(s[0] == 0) {``        ``printf``(``"\nClass A"``);``    ``}` `    ``// Condition for Class B``    ``else` `if` `(s[0] == 1 && s[1] == 0) {``        ``printf``(``"\nClass B"``);``    ``}` `    ``// Condition for Class C``    ``else` `if` `(s[0] == 1 && s[1] == 1 && s[2] == 0) {``        ``printf``(``"\nClass C"``);``    ``}` `    ``// Condition for Class D``    ``else` `if` `(s[0] == 1 && s[1] == 1 && s[2] == 1``             ``&& s[3] == 0) {``        ``printf``(``"\nClass D "``);``    ``}` `    ``// Condition for Class E``    ``else` `{``        ``printf``(``"\nClass E"``);``    ``}` `    ``return` `0;``}`

C++

 `// C++ Program to find class from``// Binary IP  address classful addressing.``#include ``using` `namespace` `std;` `// Driver Code``int` `main()``{``    ``// IP declared IPv4``    ``// 11100110.00110100.01101010.11010100``    ``int` `s[32] = { 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1,``                  ``1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0,``                  ``1, 0, 1, 1, 0, 1, 0, 1, 0, 0 };`  `    ``// Checking on all characters``    ``for` `(``int` `i = 0; i < 32; i++) {``        ``if` `(!(s[i] == 0 || s[i] == 1)) {``            ``cout << ``"Invalid"``;``            ``break``;``        ``}` `        ``if` `(i != 0 && i % 8 == 0)``            ``cout << ``" "``;` `        ``cout << s[i];``    ``}``  ` `      ``cout<

Java

 `/*package whatever //do not write package name here */``import` `java.util.Scanner;` `public` `class` `Main {``  ``public` `static` `void` `main(String[] args) {``    ``// IP declared IPv4``    ``// 11100110.00110100.01101010.11010100``    ``int``[] s = ``new` `int``[]{``1``, ``1``, ``1``, ``0``, ``0``, ``1``, ``1``, ``0``, ``0``, ``0``, ``1``,``        ``1``, ``0``, ``1``, ``0``, ``0``, ``0``, ``1``, ``1``, ``0``, ``1``, ``0``,``        ``1``, ``0``, ``1``, ``1``, ``0``, ``1``, ``0``, ``1``, ``0``, ``0``};` `    ``// Checking on all characters``    ``for` `(``int` `i = ``0``; i < ``32``; i++) {``      ``if` `(!(s[i] == ``0` `|| s[i] == ``1``)) {``        ``System.out.println(``"Invalid"``);``        ``break``;``      ``}` `      ``if` `(i != ``0` `&& i % ``8` `== ``0``) {``        ``System.out.print(``" "``);``      ``}` `      ``System.out.print(s[i]);``    ``}` `    ``// Condition for Class A``    ``if` `(s[``0``] == ``0``) {``      ``System.out.println(``"\nClass A"``);``    ``}` `    ``// Condition for Class B``    ``else` `if` `(s[``0``] == ``1` `&& s[``1``] == ``0``) {``      ``System.out.println(``"\nClass B"``);``    ``}` `    ``// Condition for Class C``    ``else` `if` `(s[``0``] == ``1` `&& s[``1``] == ``1` `&& s[``2``] == ``0``) {``      ``System.out.println(``"\nClass C"``);``    ``}` `    ``// Condition for Class D``    ``else` `if` `(s[``0``] == ``1` `&& s[``1``] == ``1` `&& s[``2``] == ``1``        ``&& s[``3``] == ``0``) {``      ``System.out.println(``"\nClass D"``);``    ``}` `    ``// Condition for Class E``    ``else` `{``      ``System.out.println(``"\nClass E"``);``    ``}``  ``}``}`

Python

 `s ``=` `[``1``, ``1``, ``1``, ``0``, ``0``, ``1``, ``1``, ``0``, ``0``, ``0``, ``1``,``     ``1``, ``0``, ``1``, ``0``, ``0``, ``0``, ``1``, ``1``, ``0``, ``1``, ``0``,``     ``1``, ``0``, ``1``, ``1``, ``0``, ``1``, ``0``, ``1``, ``0``, ``0``]` `# Checking on all characters``for` `i ``in` `range``(``32``):``    ``if` `s[i] !``=` `0` `and` `s[i] !``=` `1``:``        ``print``(``"Invalid"``)``        ``break` `    ``if` `i !``=` `0` `and` `i ``%` `8` `=``=` `0``:``        ``print``(``" "``)` `    ``print``(s[i])` `# Condition for Class A``if` `s[``0``] ``=``=` `0``:``    ``print``(``"\nClass A"``)` `# Condition for Class B``elif` `s[``0``] ``=``=` `1` `and` `s[``1``] ``=``=` `0``:``    ``print``(``"\nClass B"``)` `# Condition for Class C``elif` `s[``0``] ``=``=` `1` `and` `s[``1``] ``=``=` `1` `and` `s[``2``] ``=``=` `0``:``    ``print``(``"\nClass C"``)` `# Condition for Class D``elif` `s[``0``] ``=``=` `1` `and` `s[``1``] ``=``=` `1` `and` `s[``2``] ``=``=` `1` `and` `s[``3``] ``=``=` `0``:``    ``print``(``"\nClass D"``)` `# Condition for Class E``else``:``    ``print``(``"\nClass E"``)`

C#

 `using` `System;` `class` `MainClass {``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``// IP declared IPv4``        ``// 11100110.00110100.01101010.11010100``        ``int``[] s``            ``= ``new` `int``[] { 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1,``                          ``1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0,``                          ``1, 0, 1, 1, 0, 1, 0, 1, 0, 0 };` `        ``// Checking on all characters``        ``for` `(``int` `i = 0; i < 32; i++) {``            ``if` `(!(s[i] == 0 || s[i] == 1)) {``                ``Console.WriteLine(``"Invalid"``);``                ``break``;``            ``}` `            ``if` `(i != 0 && i % 8 == 0) {``                ``Console.Write(``" "``);``            ``}` `            ``Console.Write(s[i]);``        ``}` `        ``// Condition for Class A``        ``if` `(s[0] == 0) {``            ``Console.WriteLine(``"\nClass A"``);``        ``}` `        ``// Condition for Class B``        ``else` `if` `(s[0] == 1 && s[1] == 0) {``            ``Console.WriteLine(``"\nClass B"``);``        ``}` `        ``// Condition for Class C``        ``else` `if` `(s[0] == 1 && s[1] == 1 && s[2] == 0) {``            ``Console.WriteLine(``"\nClass C"``);``        ``}` `        ``// Condition for Class D``        ``else` `if` `(s[0] == 1 && s[1] == 1 && s[2] == 1``                 ``&& s[3] == 0) {``            ``Console.WriteLine(``"\nClass D"``);``        ``}` `        ``// Condition for Class E``        ``else` `{``            ``Console.WriteLine(``"\nClass E"``);``        ``}``    ``}``}``// This code is contributed by Prajwal Kandekar`

Javascript

 `// Javascript Equivalent``// IP declared IPv4``// 11100110.00110100.01101010.11010100``const s = [1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0];``temp=``""``;``// Checking on all characters``for` `(let i = 0; i < 32; i++) {``  ``if` `(!(s[i] === 0 || s[i] === 1)) {``    ``console.log(``"Invalid"``);``    ``break``;``  ``}` `  ``if` `(i !== 0 && i % 8 === 0) {``    ``temp=temp+``" "``;``  ``}` `  ``temp = temp +s[i] + ``""``;``}``console.log(temp);``// Condition for Class A``if` `(s[0] === 0) {``  ``console.log(``"\nClass A"``);``}` `// Condition for Class B``else` `if` `(s[0] === 1 && s[1] === 0) {``  ``console.log(``"\nClass B"``);``}` `// Condition for Class C``else` `if` `(s[0] === 1 && s[1] === 1 && s[2] === 0) {``  ``console.log(``"\nClass C"``);``}` `// Condition for Class D``else` `if` `(s[0] === 1 && s[1] === 1 && s[2] === 1 && s[3] === 0) {``  ``console.log(``"\nClass D"``);``}` `// Condition for Class E``else` `{``  ``console.log(``"\nClass E"``);``}`

Output

```11100110 00110100 01101010 11010100
Class D ```

Time Complexity: O(32).

Space Complexity: O(1) as no extra space has been used except input array.

My Personal Notes arrow_drop_up