-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExample1.java
More file actions
24 lines (20 loc) · 783 Bytes
/
Example1.java
File metadata and controls
24 lines (20 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package problem13;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Example1{
public static void main(String[] args) throws UnknownHostException{
long ip = ip_to_long(InetAddress.getByName("10.100.202.34")); // enter your ip
long mask =ip_to_long(InetAddress.getByName("255.255.240.0")); // enter your mask
ip = (ip & mask) + ((mask ^ 0xFFFFFFFFL) + 1) - 2; // result
System.out.printf("%d.%d.%d.%d", (ip >> 24) & 0xFF,(ip >> 16) & 0xFF,(ip >> 8) & 0xFF,ip & 0xFF); // output with dots, answer without it.
}
private static long ip_to_long(InetAddress ip){
byte[] octets = ip.getAddress();
long result = 0;
for(byte octet: octets){
result <<= 8;
result |= (octet & 0xFF);
}
return result;
}
}