Calculating range of IPs from subnet mask
Asked Answered
E

3

17

Say, I have a subnet of 255.255.255.242 and I have a known IP within that subnet say 192.168.1.101.

Now the way I calculate the range of IPs is this:

In the subnet mask, find the first octet that is not a 255. In my example, its the 4th octet, and its 242. So take 256 and subtract 242, which gives us 14. So we now know that these networks, the 192.168.1.x networks, all have a range of 14. So just start listing them...

192.168.1.0
192.168.1.14
192.168.1.28
....42
....56
....70
....84
....98
....112

Here we can stop. My address, 192.168.1.101 falls into the .98 network. .98 encompasses all ip addresses from 192.168.1.98 to 192.168.1.111, because we know that 192.168.1.112 starts the next network.

I want to confirm, whether this is the right and the easiest process to do so.

Elodia answered 16/10, 2012 at 15:52 Comment(2)
255.255.255.242 is not a valid subnet mask. There cannot be gaps in the bits.Hanselka
ok, Can you help me out with the calculation JoeElodia
H
43

A netmask is a series of 1 bits. The bits must be sequential with no 0 gaps. Anything using a 1 bit is part of the network, anything remaining is valid for host assignment within that network. A 255.255.255.224 has 27 "1" bits, which means it's a /27 network.

To calculate this right, you need to convert IPs to a numeric representation. For example, 255.255.255.224 is 11111111 11111111 11111111 11100000 which is 4294967264. 192.168.1.101 is 3232235877 (11000000 10101000 00000001 01100101).

If you take the IP and bitwise AND it with the netmask, that gives you the network address. This is the bottom end of the range:

11111111 11111111 11111111 11100000  (mask)
11000000 10101000 00000001 01100101  (ip)
-----------------------------------
11000000 10101000 00000001 01100000  = 192.168.1.96  (network address)

The complement (bitwise NOT) of the mask gives you the size of the range:

00000000 00000000 00000000 00011111  = 31

Thus, the range for that IP is between 192.168.1.96 - 192.168.1.127. (127 = 96 + 31)

Hanselka answered 16/10, 2012 at 18:45 Comment(2)
amazingly details, thanks a lot. But I wonder if there is a mistake: .96 to .127 makes 32 elements if i'm not mistaking. So is it .127 or .126?Threefold
You're right, because of 0-based math. There's 32 valid addresses in the range, but 0 is one of them, so the upper bound is (n+31).Hanselka
A
0

Thanks to both of you Joe and dig_123 but, Joe's answer could have been clarified with a /28 subnetinstead of the stated /27 witch would have been closer to his example and fallen between 92-112.

so, Joe if I get your point right you are saying that you take the subnetted octet; determine the increment bit value and add it to the subnet value in the SN octet, this should give the range and provide the values for the Network, first host, last host, and broadcast addresses. Is that correct? i.e. in my example the 4th octet would be a 240 and the increment would be 16. Since the value in the 4th octet is 96, it falls within the calculated range for a 16 bit increment, in fact it falls between 96 and 112 which is one of the 16 bit ranges so we can conclude that our network address for this example is:

0-15
15-31
32-47
48-63
64-79
80-95
96-111
112-127
128
NW 192.168.1.96 /28
1st 192.168.1.97 /28
Last 192.168.1.110 /28
Bcast 192.168.1.111 /28
Actinotherapy answered 30/7, 2017 at 2:31 Comment(1)
"...you take the subnetted octet..." No. Octets have nothing to do with subnetting. The dotted-decimal notation is simply to make it easier for humans to read. IPv4 addresses are simply 32-bit integers. You really need to do subnetting in binary, then everything makes sense and is easy to see. Forget about the octets when it comes to subnetting, otherwise you are setting yourself up for failure.Horgan
C
0

To add something to Joe's answer: if you want to do the operations more programmatically (assumes knowledge on bitwise operators).

You already know that only the last number will change, but this method could be used in a slightly different way for other cases as I show later.

Number from mask: 224
Number from IP:   101

Using e.g. python or your favourite calculator program:

  • Start address byte: 224 & 101 = 96
  • End address byte: (~224 & 255) | 96 = 127

(~224 & 255) just sets to one every bit that wasn't one in 244 (that is, the last 5 bits); OR'ing the result with 96 just copies the first 3 bits from the first address.

So, the result is the expected: 192.168.1.96 - 192.168.1.127.


If the mask ends before the last number there is a very similar procedure; let's do an example:

Let's use 255.224.0.0 as mask, and the same IP address (192.168.1.101).

Again there is only one number to take care of, which is now the one in second position: 168.

  • Start address byte: 224 & 168 = 160
  • End address byte: (~224 & 255) | 160 = 191

Now, the number on the left (first position) remains the same (192) and the rest of the numbers on the right range from 0 to 255 (depending on what they ask, it may also be from 1 to 254).

So the solution would be: 192.160.0.0 - 192.191.255.255

Carruthers answered 30/7, 2017 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.