Reputation: 2378
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.
Upvotes: 17
Views: 51074
Reputation: 1
Knowing the range number of subnets comes out this easy:
The number of bits you borrowed from an octet, lets say you borrowed 3 bits: It will go then like this: 2 Exp3 = 8 8 is the Number of Subnets.
The remaining bits into the chopped octet (the remaining ceros) in this case are 5 of them, right? Then 2 Exp5= 32 Then 32 - 2= 30 Number of hosts for each one of the 8 subnets you got above.
Upvotes: 0
Reputation: 1366
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
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 42666
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)
Upvotes: 43