Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a list of IP address, how do you find min, max?

In Java, i have an arrayList of ip address. how do i find the min and max ?

I have used the Collection.min() but it doesnt work given a case like :

192.168.0.1  <--min 
192.168.0.250
192.168.0.9  <--max

how do i return

192.168.0.1  <--min
192.168.0.250 <--max

instead?


ArrayList is retrieve from the database. I would need to do this operation per tick (each tick is at 5 sec interval). The number of IP address would hit a max of probably 300.

like image 831
zeroin23 Avatar asked May 11 '10 08:05

zeroin23


People also ask

What is the minimum and maximum IP address?

IPv4 Addresses Integers "0-9", ".", and "-" can be part of an IP address range. The range of IP addresses must be between 0.0. 0.0 and 255.255. 255.255.

How do you find the maximum number of hosts from an IP address?

The maximum number of hosts can be determined from the number of hosts available in the network, It is the number obtained by subtracting two of less and broadcast address.

How do you find the range of an IP address?

Using the formula 2H-2 to calculate usable IPs, we get the following: Class A = 224 – 2 = 16,777,214 total IPs. Class B = 216 – 2 = 65,534 total IPs. Class C = 28 – 2 = 254 total IPs.

How do you read a 24 IP range?

/24 indicates a subnet mask of 255.255. 255.0, or in binary octets. A Subnet mask is a 32-bit number that masks an IP address, and divides the IP address into network address and host address. Subnet Mask is made by setting network bits to all "1"s and setting host bits to all "0"s.


1 Answers

Convert the IP address into a long integer, then sort it. 192.168.0.1 can be converted to an integer using binary arithmetic/operators:

( 192 << 24 ) + ( 168 << 16 ) + ( 0 << 8 ) + ( 1 << 0 )

and so on. Do read the comments below about using the correct data type.

like image 145
Salman A Avatar answered Sep 17 '22 17:09

Salman A