How do you programmatically find the number of hosts that a netmask supports.
Eg, If you have a /30 , how do you find how many IP’s are in it without using a lookup table?
Preferably would be able to work with the ‘/’ notation, rather than 255.xxx.xxx.xxx notation.
Here’s the formula: 2 ^ (32 – netmask) – 2 where netmask is a bit count as you’ve shown in the Cisco notation above. So a network with a /30 mask has 2 usable addresses.
The lowest network number always represents the network segment itself and the highest is always the broadcast … this leads to the -2 at the end of the formula.
For standard notation, convert the aaa.bbb.ccc.ddd netmask into an unsigned 4 byte integer (many networking libraries have this function) and subtract that from 2 ^ 32 – 2.