I am having trouble with some of the logic in converting an IP Address range into a list of CIDR blocks. I do believe that this website is doing it right: http://ip2cidr.com
You need to understand binary numbers, nothing more.
An CIDR block is nothing else than a series of binary numbers with common prefix and all possible suffixes.
Assume for the example below we had 8-bit IP-addresses, with classes /1
, ... to /8
.
In your case (ignoring the 1.1.1 for now), we write your numbers as binary numbers:
1101111 - 111
1110000 - 112
1110001 - 113
...
1110110 - 118
1110111 - 119
1111000 - 120
You'll see that all numbers have a common 11
prefix, but our list does not contain all these numbers. So we have to split it in two lists - one with 110
and one with 111
. The first contains only one number, so we make a /8
block out of it (111/8
).
The other list (from 112 to 120) contains not all numbers with 111
(since then it would go up to 127), so we split again - one list with 1110
, the other with 1111
. The first one is now the complete block 1110????
(or 112/4
), the second one is only one single address, namely 11111000
(or 120/8
).
So, now only extend to 32 bit instead of 8, and implement in Java, and you are ready.
In mathematical terms, one block always goes from x * 2^n to (x+1) * 2^n - 1, and we then use 32 - n as the block-size suffix. So you only need to find the next multiple of some power of two.