IPv4 addresses are 32 bits wide, and thus the size of the address space is 232, or 4,294,967,296. However, this is only a theoretical upper-bound. It is not an accurate representation of all the addresses that may actually be used on the public internet.

For the purposes of this challenge, it is assumed that all addressing is classful. In reality, classful subdivision of address space has been superseded by CIDR (Classless Inter-Domain Routing and VLSM (Variable Length Subnet Masking), but this is ignored for this challenge.

According to the classful address scheme, there are 3 classes:

  • Class A - to with /8 netmask length
  • Class B - to with /16 netmask length
  • Class C - to with /24 netmask length

Classes D (multicast) and E (reserved) are also defined, but these are not used for public unicast addresses.

Each class is subdivided into networks according to the netmask for that class.

Thus is an example of a Class A network. The netmask length for Class A is 8, so the full address space for this network is to However, the first address ( is reserved as the network address and the last address ( is reserved as the broadcast address for that network. Thus the actual range of usable addresses is to which is 224 - 2 (= 16,777,214) total addresses.

Similarly, is an example of a Class C network. The netmask length for Class C is 24, so the full address space for this network is to Removing the network and broadcast addresses leaves the actual range of usable addresses is to which is 28 - 2 (= 254) total addresses.

There are further limitations on address ranges that may be used for public unicast. According to RFC 6890, the disallowed ranges are:

  • - Local networking
  • - Private-Use
  • - Shared Address Space
  • - Loopback
  • - Link Local
  • Private-Use
  • - IETF Protocol Assignments
  • - Reserved for use in documentation
  • - 6to4 Relay Anycast
  • - Private-Use
  • - Benchmarking
  • - Reserved for use in documentation
  • - Reserved for use in documentation

Note that the above list uses VLSR netmasks to efficiently specify a range. In all but one cases, the given mask length has specificity is less than or equal to the normal classful mask length for the start of the range. Thus each of these VLSR ranges is equivalent to one or more classful networks. E.g. is equivalent to the Class B networks to or the address range to

The exception to this rule is the VLSR range, which is more specific than the containing Class A range. Thus will be handled like other Class A ranges with the exception that it has a 4,194,304-address hole in the middle. The valid addresses in this Class A range will be to and to, a total of 224 - 222 - 2 (= 12,582,910) total addresses.

The goal of this challenge is to output all Class A, B and C unicast IPv4 addresses that may be validly assigned to a public internet host (i.e. excluding those detailed above).

  • No input will be given and should not be expected.

  • Output may be in any form convenient for your language, e.g. array, list, delimited string. Addresses must be output in standard dotted decimal format.

  • Output order does not matter.

  • Builtins that specifically give the required ranges of addresses are disallowed. Similarly any methods to dynamically inspect a BGP (or other protocol) routing table for the public internet are disallowed.

The numerically lowest address will be and the numerically highest will be

This challenge is similar to Print out all IPv6 addresses, but because of the restrictions should require non-trivially different implementation.


2 Answers 2


PowerShell, 648 641 625 bytes


Edit 1 -- I've golfed out all remaining powers-of-two operators, which saved an additional 7 bytes.
Edit 2 -- Moved the [uint64] cast to the first declaration of $a which eliminated the other two re-casts which saved 16 bytes.

Three lines, Class A / Class B / Class C. Left as separate lines for readability. ;-)

Two key points for understanding what's going on:

  • PowerShell has powers-of-two operators KB, MB, GB. For example, 4KB will return 4096 as an int. We leverage that in multiple locations to shave dozens of bytes.
  • The .NET [ipaddress] class will try to parse a numerical value as an IP address by taking the number's binary representation. We use that constructor with the IPAddressToString argument for output.

By coupling those two things, we're able to just treat the IP addresses as numbers and loop through them with a for() loop. For example, the first loop for Class A subnets goes from 16MB to 2GB-16MB, or from 16777216 to 2130706432. The binary representation of 16777216 is 1000000000000000000000000 or 00000001.00000000.00000000.00000000 if we split it into 8-bit chunks so we can easily see that corresponds to in dotted-decimal notation. Similarly, 2130706432 can be written as 01111111000000000000000000000000 or 01111111.00000000.00000000.00000000 or Each integer, or power-of-two integer, used here can be rewritten as an IP address in this fashion.

So, for each loop iteration, we construct an if() statement to weed out the excluded addresses, by multiplying the individual statements together. Since the first statement in each if is an integer (thanks to the modulo testing), the remaining Boolean values are converted to either 0 or 1 for false/true. If any one of the statements are false, the whole multiplication will turn to 0 and thus be false. Thus, only if all the statements are true, will we output the result of the parsing.

Slightly Ungolfed:

# Class A
  $b=($a%16mb)                     # x.0.0.0
  $b*=(($a+1)%16mb)                # x.255.255.255
  $b*=($a-lt160MB-or$a-gt176MB)    #
  $b*=($a-lt1604MB-or$a-ge1608MB)  #

# Class B
  $b=($a%64kb)                           # x.y.0.0
  $b*=(($a+1)%64kb)                      # x.y.255.255
  $b*=(($a-lt2785152kb-or$a-gt2720mb))  #
  $b*=(($a-lt2753mb-or$a-gt2754mb))      #

# Class C
  $b=($a%256)                               # x.y.z.0
  $b*=(($a+1)%256)                          # x.y.z.255
  $b*=(($a-lt3221225984-or$a-gt3221226240)) #
  $b*=(($a-lt3227017984-or$a-gt3151385kb)) #
  $b*=(($a-lt3156480kb-or$a-gt3156544kb)) #
  $b*=(($a-lt3245184kb-or$a-gt3245312kb)) #
  $b*=(($a-lt3247321kb-or$a-gt3325256959)) #
  $b*=(($a-lt3405803776-or$a-gt3405804032)) #

Batch, 1930 1884 1848 1830 bytes

@echo off
for /l %%a in (1,1,9)do call:a1 %%a
for /l %%a in (11,1,99)do call:a1 %%a
for /l %%b in (0,1,63)do call:a2 100 %%b
for /l %%b in (128,1,255)do call:a2 100 %%b
for /l %%a in (101,1,126)do call:a1 %%a
for /l %%a in (128,1,168)do call:b1 %%a
for /l %%b in (0,1,253)do call:b2 169 %%b
call:b2 169 255
call:b1 170
call:b1 171
for /l %%b in (0,1,15)do call:b2 172 %%b
for /l %%b in (32,1,255)do call:b2 172 %%b
for /l %%a in (173,1,191)do call:b1 %%a
call:c3 192 0 1
for /l %%c in (3,1,255)do call:c3 192 0 %%c
for /l %%b in (1,1,87)do call:c2 192 %%b
for /l %%c in (0,1,98)do call:c3 192 88 %%c
for /l %%c in (100,1,255)do call:c3 192 88 %%c
for /l %%b in (89,1,167)do call:c2 192 %%b
for /l %%b in (169,1,255)do call:c2 192 %%b
for /l %%a in (193,1,197)do call:c1 %%a
for /l %%b in (0,1,17)do call:c2 198 %%b
for /l %%b in (20,1,50)do call:c2 198 %%b
for /l %%c in (0,1,99)do call:c3 198 51 %%c
for /l %%c in (101,1,255)do call:c3 198 51 %%c
for /l %%b in (52,1,255)do call:c2 198 %%b
for /l %%a in (199,1,202)do call:c1 %%a
for /l %%c in (0,1,112)do call:c3 203 0 %%c
for /l %%c in (114,1,255)do call:c3 203 0 %%c
for /l %%b in (1,1,255)do call:c2 203 %%b
for /l %%a in (204,1,223)do call:c1 %%a
for /l %%b in (0,1,255)do call:a2 %1 %%b
for /l %%c in (0,1,255)do call:a3 %1 %2 %%c
for /l %%d in (0,1,255)do if not %2%3%%d==000 if not %2%3%%d==255255255 echo %1.%2.%3.%%d
for /l %%b in (0,1,255)do call:b2 %1 %%b
for /l %%c in (0,1,255)do call:b3 %1 %2 %%c
for /l %%d in (0,1,255)do if not %3%%d==00 if not %3%%d==255255 echo %1.%2.%3.%%d
for /l %%b in (0,1,255)do call:c2 %1 %%b
for /l %%c in (0,1,255)do call:c3 %1 %2 %%c
for /l %%d in (1,1,254)do echo %1.%2.%3.%%d

Edit: Saved 46 82 bytes by removing unnecessary spaces. Saved 18 bytes by using exit/b instead of goto:eof.

  • 1
    \$\begingroup\$ I count 1872 bytes. You technically don't need the @echo off, as well. \$\endgroup\$ Commented Jan 29, 2016 at 17:29
  • \$\begingroup\$ @FlagAsSpam Probably CRs; Notepad likes to save them. \$\endgroup\$
    – Neil
    Commented Jan 29, 2016 at 17:40
  • \$\begingroup\$ I think you can remove them, since we count by Unix UTF-8 bytes. \$\endgroup\$ Commented Jan 29, 2016 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.