Take the 2-minute tour ×  I have an ip array like below and I
want to convert it to smallest cidr subnet list. Is there a
library for that in Java?  For example:  converted to  Thanks in
advance.  share|improve this question  2 Answers 2  up vote 1
down vote accepted  I don't know whether there is a library
available in Java. Indeed, I know little about Java :) But I can
give you an algorithm for solving the problem, if that is any
help.  1) Convert the ip addresses into pairs of integers, where
the first integer is the binary representation of the ip address
(a.b.c.d -> a << 24 + b << 16 + c << 8 + d) and the second
integer is 32 (that is, initially each address is it's own
subnet [1]).  2) Sort the list of pairs.  3) Now scan the sorted
list, starting at the second pair. For each pair, if you can
combine it with the previous one, do so and continue to try
until combining as long as possible. Two pairs [base1, bits1]
and [base2, bits2] can be combined if bits1 == bits2 and base2 -
base1 == 1 << (32 - bits1).