What would be a efficient way to generate all possible IP v4 addresses? other than iterating all bytes in a one giant nested for loop.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Edit: My previous answer would have gone from
128.0.0.0to255.255.255.255to0.0.0.0to127.255.255.255. Presumably you want to go from0.0.0.0to255.255.255.255, so I’ve edited my solution to do that.Note: if you’re confused how this loop will ever end (i.e. how adding 1 to -1 enough times makes it -1 again), read up on two’s complement. Basically, adding one to
Integer.MAX_VALUEresults inInteger.MIN_VALUE, and does not throw any kind of exception.Old answer. Still hits all IPs, but probably not in the order you desire:
Please note: If the loop control variable was an
intinstead of along, this would be an infinite loop (since allints are always<= Integer.MAX_VALUE).