import java.net.*; //for class InetAddress in lines 12-13 class Class { static public void main(String argv[]) { if (argv.length != 1) { System.err.println( "Class: argument must be a dotted IPv4 address"); System.exit(1); } try { final InetAddress ip = InetAddress.getByName(argv[0]); final byte[] b = ip.getAddress(); //array of 4 bytes final byte i = b[0]; //the high order byte char c; //IPv4 address class: A, B, C, D, or E if ((i & 1 << 7) == 0) { c = 'A'; //Bit 31 is 0 (includes loopback address). } else if ((i & 1 << 6) == 0) { c = 'B'; //Bit 31 is 1, bit 30 is 0. } else if ((i & 1 << 5) == 0) { c = 'C'; //Bit 31 is 1, bit 30 is 1, bit 29 is 0. } else if ((i & 1 << 4) == 0) { c = 'D'; //Bit 31 is 1, bit 30 is 1, 29 is 1, 28 is 0. } else { c = 'E'; //Bit 31 is 1, bit 30 is 1, 29 is 1, 28 is 1. } System.out.println(argv[0] + " is a class " + c + " IPv4 address."); System.exit(0); } catch (Exception e) { //may be thrown by getByName in line 12 System.err.println("argument " + argv[0] + " must be a dotted IPv4 address"); System.exit(2); } } }