# How to put two subnets next to each other?

I've been using this calculator http://www.subnet-calculator.com/cidr.php, and I'm trying to figure out how to put two different subnets next to each other.

For example, I want to have a /27 subnet starting at 1.0.0.1, next to a /25, so I though that that the /25 subnet would start at 1.0.0.32, as that is one outside the /27 subnet. However, when I try and do this, the calculator says that the range for /25 would be 1.0.0.1-1.0.0.127, not starting at .32.

Is this a calculator limitation or how do you put subnets next to each other?

EDIT: I guess my question is what subnets can go next to each other? What determines this?

-
You cannot have a /25 starting at 1.0.0.32, if that what you are asking. Remember that subnets are not arbitrary ranges, they are merely a way to distinguish host number from network number. – BatchyX Apr 18 '13 at 14:08
I guess my question is what subnets can go next to each other? Is there a document or reference on which can go where? – instipod Apr 18 '13 at 14:09

You need to distinguish the subnet start address and the subnet size. The number behind the slash is the size (in 32-x bits). So you can have two /27 subnets like this

``````10.0.0.1/27  == 10.0.0.1  -> 10.0.0.30
10.0.0.33/27 == 10.0.0.33 -> 10.0.0.62
``````

but a /27 and a /25 subnet in the same way would mean starting the /25 at a later address

``````10.0.0.1/27   == 10.0.0.1   -> 10.0.0.30
10.0.0.129/25 == 10.0.0.129 -> 10.0.0.254
``````

since the /25 subnet "needs" more space. You cannot start the /25 subnet at an arbitrary address, only at the correct boundaries:

``````10.0.0.1/25   == 10.0.0.1   -> 10.0.0.126
10.0.0.129/25 == 10.0.0.129 -> 10.0.0.254
``````

but note that

``````10.0.0.33/25   == 10.0.0.1   -> 10.0.0.126
``````

because `10.0.0.33/25` is just another way of saying `10.0.0.1/25` or `10.0.0.0/25`.

You could also decide to "fill" the space between you /27 and your /25 subnet with more /27 subnets:

``````10.0.0.1/27   == 10.0.0.1   -> 10.0.0.30
10.0.0.33/27  == 10.0.0.33  -> 10.0.0.62
10.0.0.65/27  == 10.0.0.65  -> 10.0.0.94
10.0.0.97/27  == 10.0.0.97  -> 10.0.0.126
10.0.0.129/25 == 10.0.0.129 -> 10.0.0.254
``````

or with another /27 and a /26:

``````10.0.0.1/27   == 10.0.0.1   -> 10.0.0.30
10.0.0.33/27  == 10.0.0.33  -> 10.0.0.62
10.0.0.65/26  == 10.0.0.65  -> 10.0.0.126
10.0.0.129/25 == 10.0.0.129 -> 10.0.0.254
``````
-
Subnets include the network address and the broadcast address... you don't have unused IP's between contiguous subnets. And trying to explain why some subnets can be combined into a shorter mask without binary math is completely misleading. – cpt_fink Apr 19 '13 at 7:27

Prefixes/subnets use binary logic. Subnets are determined by the bits that are fixed and the bits that are usable for addresses. The number of fixed bits is the prefix length or subnet mask. A few IPv4 examples:

``````Prefix:           10.0.0.0/8
Prefix length:    8
Prefix bits:      00001010 00000000 00000000 00000000 = 10.0.0.0
Subnet mask bits: 11111111 00000000 00000000 00000000 = 255.0.0.0
``````

A `1` in the subnet mask bits indicates that the corresponding bit is fixed, and a `0` indicates that you can use that bit. The prefix length is the number of bits set to `1`, and the subnet mask is that binary number written down as an IPv4 address.

So in this example you can use:

``````First address in the prefix: 00001010 00000000 00000000 00000000 = 10.0.0.0
Last address in the prefix:  00001010 11111111 11111111 11111111 = 10.255.255.255
``````

Another example with a different prefix length:

``````Prefix:           10.0.0.0/10
Prefix length:    10
Prefix bits:      00001010 00000000 00000000 00000000 = 10.0.0.0
Subnet mask bits: 11111111 11000000 00000000 00000000 = 255.192.0.0
``````

In this example you can use less addresses:

``````First address in the prefix: 00001010 00000000 00000000 00000000 = 10.0.0.0
Last address in the prefix:  00001010 00111111 11111111 11111111 = 10.63.255.255
``````

As you can see the subnet is determined by the value and number of the fixed bits. When using your example `1.0.0.32/25` you get:

``````Prefix:           1.0.0.32/25
Prefix length:    25
Prefix bits:      00000001 00000000 00000000 00100000 = 10.0.0.32
Subnet mask bits: 11111111 11111111 11111111 10000000 = 255.255.255.128

First address in the prefix: 00000001 00000000 00000000 00000000 = 1.0.0.0
Last address in the prefix:  00000001 00000000 00000000 01111111 = 1.0.0.127
``````

The value 32 is in the middle of the flexible bits. When looking at `/25` prefixes you get:

``````Prefix length:      25
Subnet mask bits:   11111111 11111111 11111111 10000000

1st /25 in 1.0.0.0: 00000001 00000000 00000000 00000000 = 1.0.0.0/25
2nd /25 in 1.0.0.0: 00000001 00000000 00000000 10000000 = 1.0.0.128/25
3rd /25 in 1.0.0.0: 00000001 00000000 00000001 00000000 = 1.0.1.0/25
4th /25 in 1.0.0.0: 00000001 00000000 00000001 10000000 = 1.0.1.128/25
5th /25 in 1.0.0.0: 00000001 00000000 00000010 00000000 = 1.0.2.0/25
Etc.
``````

When looking at `/27` prefixes you get:

``````Prefix length:      27
Subnet mask bits:   11111111 11111111 11111111 11100000

1st /25 in 1.0.0.0: 00000001 00000000 00000000 00000000 = 1.0.0.0/27
2nd /25 in 1.0.0.0: 00000001 00000000 00000000 00100000 = 1.0.0.32/27
3rd /25 in 1.0.0.0: 00000001 00000000 00000000 01000000 = 1.0.0.64/27
4th /25 in 1.0.0.0: 00000001 00000000 00000000 01100000 = 1.0.0.96/27
5th /25 in 1.0.0.0: 00000001 00000000 00000000 10000000 = 1.0.0.128/27
Etc.
``````

In an IPv4 subnet the first address (flexible bits all `0`) is reserved and called the network address. The last address (flexible bits all `1`) is the subnet broadcast address. You can't use those for network interfaces on devices.

If you want to put multiple subnets next to each other you'll have to make sure that they don't overlap. When you don't have a lot of address space like with IPv4 making all the subnets fit can be a very difficult process, and keeping it manageable when changing the addressing plan is even harder. That is why IPv6 is so nice to work with: plenty of address space, and a subnet is usually a `/64` (it is possible to use different prefix lengths but that does break some things like auto-configuration).

If you are interested in IPv6 addressing plans then take a look at the 'Preparing an IPv6 Addressing Plan' document I wrote a couple of years ago for SURFnet (the Dutch National Research and Education Network). The way subnetting works in IPv6 is exactly the same as for IPv4, except the numbers are a lot bigger and written in hexadecimal (which corresponds much better to bits than the decimal notation used for IPv4!). Prefix lengths, having fixed and flexible bits all works in exactly the same way though. A short example:

``````Prefix:           2001:0db8:0000:0000:0000:0000:0000:0000/64
Prefix length:    64
Subnet mask:      not really used anymore in IPv6, but it would have been:
ffff:ffff:ffff:ffff:0000:0000:0000:0000
Prefix bits:      0010 0000 0000 0001 0000 1101 1011 1000 = 2001:0db8
0000 0000 0000 0000 0000 0000 0000 0000 = 0000:0000
0000 0000 0000 0000 0000 0000 0000 0000 = 0000:0000
0000 0000 0000 0000 0000 0000 0000 0000 = 0000:0000
Subnet mask bits: 1111 1111 1111 1111 1111 1111 1111 1111 = ffff:ffff
1111 1111 1111 1111 1111 1111 1111 1111 = ffff:ffff
0000 0000 0000 0000 0000 0000 0000 0000 = 0000:0000
0000 0000 0000 0000 0000 0000 0000 0000 = 0000:0000

0010 0000 0000 0001 0000 1101 1011 1000 = 2001:0db8
0000 0000 0000 0000 0000 0000 0000 0000 = 0000:0000
0000 0000 0000 0000 0000 0000 0000 0000 = 0000:0000
0000 0000 0000 0000 0000 0000 0000 0000 = 0000:0000
0010 0000 0000 0001 0000 1101 1011 1000 = 2001:0db8
0000 0000 0000 0000 0000 0000 0000 0000 = 0000:0000
1111 1111 1111 1111 1111 1111 1111 1111 = ffff:ffff
1111 1111 1111 1111 1111 1111 1111 1111 = ffff:ffff

So from 2001:0db8:0000:0000:0000:0000:0000:0000
to 2001:0db8:0000:0000:ffff:ffff:ffff:ffff
``````

PS: I didn't use the recommended/canonical notation here on purpose. Usually you compress the zeroes in the address and write `2001:0db8:0000:0000:0000:0000:0000:0000` as `2001:db8::`, `2001:0db8:0000:0000:0000:0000:0000:0001` is written as `2001:db8::1`, etc.

-
• For a /24, the last octet (usually reserved) for the network is .0 and only .0. `1 subnet`

• For a /25, it may then be either .0 or .128. `2 subnets`

• For a /26, it may be either .0, .64, .128, or .192. `4 subnets`

• For a /27, it may be either .0, .32, .64, .96, .128, .160, .192, or .224. `8 subnets`

• for a /28, .0, .16, .32, .48, .64, .80, .96, .112, .128, .144, .160, .176, .192, .208, .224, or .240. `16 subnets`

• for a /29, .0, .8, .16, .24, .32, .40, .48, .56, .64, .72, .80, .88, .96, .104, .112, .120, .128, .136, .144, .152, .160, .168, .176, .184, .192, .200, .208, .216, .224, .232, .240, or .248 `32 subnets`

• The /30 prefix is usually found on point-to-point interfaces. ```64 subnets```

• The /31 prefix is not commonly found in the wild, as it has no commonly addressable hosts, since it only spans 2 network numbers, the "network" and the "broadcast" with no space for a host IP. `128 subnets` (all even numbers between 0 and 254)

• The /32 prefix is used to specify a route for a single host. It's the most specific of routes and if present, should take routing precedence over all other route table entries that are not also /32s. A /32 does not have a 'network' nor a 'broadcast' address. `256 subnets` (0 and 255 may not work on some implementations)

-

A simple way to understand it :

In IPv4:

Imagine a line of 256*256*256*256 (Or 2^32) possible IP adresses.

``````[] [] [] [] .................. [] [] []
``````

This has subnet mask 0.0.0.0 (or 0000 0000 0000 0000 0000 0000 0000 0000 in binary)
All the bits that are not masked can be used to give an IP adress in that network.

The possible adresses in that single net are:

0000 0000 0000 0000 0000 0000 0000 0000 (<-- NETMASK, not masking anything here...)

0000 0000 0000 0000 0000 0000 0000 0000 (IP 0.0.0.0) to
1111 1111 1111 1111 1111 1111 1111 1111 (IP 255.255.255.255)

This whole network start at IP 0.0.0.0, and goes until IP 255.255.255.255

Each bit in a subnet mask will divide the line in 2 equal parts.

The first bit in a subnet mask will divide this in 2 equal parts, each with 128*256*256*256 (or 2^31) IP adresses :

``````[] [] [] .......... [] [] []  |  [] [] ........... [] []
``````

This has subnet mask 128.0.0.0 (or 1000 0000 0000 0000 0000 0000 0000 0000 in binary)
All the bits that are not masked can be used to give an IP adress in that network.

So you can have 2 subnets, and for each subnets, you have 31 bits of available IP adresses.

For the first subnet (the one where, behind the netmask, is '0')

1000 0000 0000 0000 0000 0000 0000 0000 (<-- NETMASK)

0000 0000 0000 0000 0000 0000 0000 0000 (IP 0.0.0.0) to
0111 1111 1111 1111 1111 1111 1111 1111 (IP 127.255.255.255)

and for the 2nd subnet (the one where, behind the netmask, is '1')

1000 0000 0000 0000 0000 0000 0000 0000 (<-- NETMASK)

1000 0000 0000 0000 0000 0000 0000 0000 (IP 128.0.0.0) to
1111 1111 1111 1111 1111 1111 1111 1111 (IP 255.255.255.255)

The next additionnal bit in the subnet mask divides both sides in 2 equal parts of 2^30 IP Adresses each

And so on...

So if you try to assign, say, a subnet of /3, it means you spent 3 iteration dividing, ending up with 2^3=8 subnets. Each subnet can only be one of the 8 subdivision of the whole line of machines. They can't overlap. Each one starting after the former one.

``````[] ... [] | [] ... [] | [] ... [] | [] ... [] | [] ... [] | [] ... [] | [] ... [] | [] ... []
32*256*256*256 or 2^30 IP Adresses each.
``````

So for the first subnet (the one where, behind the netmask, is '000')

1110 0000 0000 0000 0000 0000 0000 0000 (<-- NETMASK)

0000 0000 0000 0000 0000 0000 0000 0000 (IP 0.0.0.0) to
0001 1111 1111 1111 1111 1111 1111 1111 (IP 31.255.255.255)

and for the 2nd subnet (the one where, behind the netmask, is '001')

1110 0000 0000 0000 0000 0000 0000 0000 (NETMASK)

0010 0000 0000 0000 0000 0000 0000 0000 (IP 32.0.0.0) to
0011 1111 1111 1111 1111 1111 1111 1111 (IP 63.255.255.255)

...

and for the 7th subnet (the one where, behind the netmask, is '110')

1110 0000 0000 0000 0000 0000 0000 0000 (NETMASK)

1100 0000 0000 0000 0000 0000 0000 0000 (IP 192.0.0.0) to
1101 1111 1111 1111 1111 1111 1111 1111 (IP 223.255.255.255)

and for the 8nd subnet (the one where, behind the netmask, is '111')

1110 0000 0000 0000 0000 0000 0000 0000 (NETMASK)

1110 0000 0000 0000 0000 0000 0000 0000 (IP 224.0.0.0) to
1111 1111 1111 1111 1111 1111 1111 1111 (IP 255.255.255.255)

IF you continue to add bit to the netmask, you continue divising: A subnet of /32 singles out a single machine.

But remember you can't really have only machines:

to make things work, some of the subnet's range are reserved:

for each subnet, the "0 bit at value 1" and "all bits at value 1" are usually reserved for broadcasting, so you usually have only nb_of_possible_adresses_in_the_subnet-2 IP adresses available in a subnet for actual machine interfaces. And one should better be the interface of a gateway which has another interface in other net(s), allowing you to use it as a gateway to reach those other nets (and everything, via those other net's gateways)

-
tough to represent neatly... And I hope the confusion between the "line of 2^32 IP adresses" and the 32 bit representation used everywhere else are not more confusing than helping... – Olivier Dulac Apr 18 '13 at 20:46