Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 523223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:26:08+00:00 2026-05-13T08:26:08+00:00

What would be a efficient way to generate all possible IP v4 addresses? other

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:26:08+00:00Added an answer on May 13, 2026 at 8:26 am

    Edit: My previous answer would have gone from 128.0.0.0 to 255.255.255.255 to 0.0.0.0 to 127.255.255.255. Presumably you want to go from 0.0.0.0 to 255.255.255.255, so I’ve edited my solution to do that.

    int i = -1;
    do {
      i++;
    
      int b1 = (i >> 24) & 0xff;
      int b2 = (i >> 16) & 0xff;
      int b3 = (i >>  8) & 0xff;
      int b4 = (i      ) & 0xff;
    
      //Now the IP is b1.b2.b3.b4
    
    } while(i != -1);
    

    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_VALUE results in Integer.MIN_VALUE, and does not throw any kind of exception.


    Old answer. Still hits all IPs, but probably not in the order you desire:

    for(long n = Integer.MIN_VALUE; n <= Integer.MAX_VALUE; n++)
    {
      int i = (int)n;
    
      int b1 = (i >> 24) & 0xff;
      int b2 = (i >> 16) & 0xff;
      int b3 = (i >>  8) & 0xff;
      int b4 = (i      ) & 0xff;
    
      //Now the IP is b1.b2.b3.b4
    }
    

    Please note: If the loop control variable was an int instead of a long, this would be an infinite loop (since all ints are always <= Integer.MAX_VALUE).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be the most efficient way to first generate and then sum a
What would be the most efficient way of recording to a log (.txt) from
What would be the most efficient way of counting the number of times a
What would be the most elegant and efficient way of finding/returning the first list
Would anyone please be able to tell me the most efficient way to compare
I would like to ask you what's the most efficient way for checking user's
In SQL Server would a varbinary(16) be the most efficient way of storing an
I have this string: B82V16814133260 what would be the most efficient way to get
If I have two associative arrays, what would be the most efficient way of
I'm looking for a more efficient way to generate some DOM changes and update

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.