How to calculate subnet mask for a certain number of hosts

cosmicempathy

Honorable
Oct 25, 2013
7
0
10,510
Does anyone know a good formula for figuring out the mask for a maximum number of hosts per network segment? For example, I have a class B network and I need to figure out the mask for 350 users per segment... not sure how to go beyond 254 hosts... I know that the mask would be 255.255.254.0, I just need to figure out a formula for how to get there.
 
Solution
Unfortunately, there's no easy way to post mathematical formulas here.

The possible number of addresses in a network is given by 2 to the power of the number of bits that can vary. Through algebra, you can show that the number of bits needed for a given number of addresses is the base 2 logarithm of the number of bits. This is sometimes abbreviated to 'lg'.

Google: lg(512). That should give you the answer of nine, which is how many bits have to vary.

You then set the last 9 bits of a 32bit sequence to zero:
11111111111111111111111000000000

Split it into 4 groups of 8 bits:
11111111 11111111 11111110 00000000

Convert each byte into decimal:
255 255 254 0

And dot it:
255.255.254.0
The subnet mask expands to binary (in some systems it's written as the number of bits in a row set to one, e.g. 255.255.255.0 would be /24).

255.255.255.0 becomes 11111111 11111111 11111111 00000000. As you can see, the first 24 bits are set to one. If a bit is set to one, the bits in the address must match.

$addresses = 2^$zeroes, or $zeroes = log(2)$addresses.

Note that you need to round up to the next power of two, e.g. 512 addresses (with 510 hosts, because of excluding the 0 and 255 addresses.

If you were using an address of e.g. 192.168.0.xxx, valid addresses would be 192.168.0.1 to 192.168.1.254, with a mask of 255.255.254.0 - a /23 subnet.
 

cosmicempathy

Honorable
Oct 25, 2013
7
0
10,510


Hi, Thanks for your reply... but I am not understanding your formula of $addresses = 2^$zeroes, or $zeroes = log(2)$addresses.

I understand how a subnet mask works, how to find the ranges, etc. I just need to know how to figure out the mask when given a number of hosts.

Can you show me visually how you came up with 512 (- 2, of course) and how the mask comes out to be 255.255.254.0?
 
Unfortunately, there's no easy way to post mathematical formulas here.

The possible number of addresses in a network is given by 2 to the power of the number of bits that can vary. Through algebra, you can show that the number of bits needed for a given number of addresses is the base 2 logarithm of the number of bits. This is sometimes abbreviated to 'lg'.

Google: lg(512). That should give you the answer of nine, which is how many bits have to vary.

You then set the last 9 bits of a 32bit sequence to zero:
11111111111111111111111000000000

Split it into 4 groups of 8 bits:
11111111 11111111 11111110 00000000

Convert each byte into decimal:
255 255 254 0

And dot it:
255.255.254.0
 
Solution