Hello;

What would be the most efficient way to get all of the 2^24 network identifiers for the given A class IP address. I am aware it is a very stupid question, and a inpractical one for that matter but thank you for any input. Im trying to speed up and enhance this algorithm here :

``````/**
* @author joey
*
*         Example subnet -> 13.0.0.0/31 where 13 is a
* @param octet
*            houses the subnet mask octet index which holds last ON
*            bit [values range from 1-4, 4 being the last octet]
* @param tempo
*            houses the last ON bit in the subnet mask converted to
*            decimal which we then use as a step between network
*            ids [possible values are 256,128,64,32,16,8,4,2,1]
* @param (in1, in2, in3, i, j, k) various helper vars
* @param z
*            counter var
* @param a
* */
if (octet == 4) {
in3 = tempo;
in1 = in2 = 1;
for (i = z = 0; i < 256; i += in1)
for (j = 0; j < 256; j += in2)
for (k = 0; k < 256; k += in3, z++) {
System.out.println(a + "." + i + "." + j + "." + k);
}
}``````

Edited by Joey_Brown: n/a

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Joey_Brown

You do realise thats 16 million addresses?

You do realise thats 16 million addresses?

Yes, like I said I'am very well aware that I've posted a very stupid and inpractical query. This is not ment to be implemented in a production system; Im just personally curious how and could it be done differently.

OK, in that case I can't see how you would improve on triple-nested 0-255 loops. You may be able to squeeze some more speed by doing some of the string conversion/formatting in the outer loops, eg so you don't evaluate `a + "." + i + "." + j + "."` 255 times with exactly the same values...
but in reality you would have to benchmark to be sure.

OK, in that case I can't see how you would improve on triple-nested 0-255 loops. You may be able to squeeze some more speed by doing some of the string conversion/formatting in the outer loops, eg so you don't evaluate `a + "." + i + "." + j + "."` 255 times with exactly the same values...
but in reality you would have to benchmark to be sure.

Thank you for the input mr. James. Im gonna go research and test the problem some more.