ip address math

I'm trying to figure out how many addresses fit into a range.  For example, how many addresses are in the range -  Is there a formula I can use to figure out the number of possible addresses from range1 - range 2?  
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

UrosVidojevicConnect With a Mentor Commented:
In this case: ->

(128*256^3 + 100*256^2 + 255*256 + 255) - (128*256^3 + 100*256^2 + 0*255 + 0)
= 65536,

Pay attention that, in this subnet and can be used...
A.B.C.D -> P.Q.R.S

(P*256^3 + Q*256^2 + R*256 + S) - (A*256^3 + B*256^2 + C*256 + D) + 1
Kent OlsenData Warehouse Architect / DBACommented:
Hi npinfotech,

Sure.  :)

Think of the IP address for exactly what it is.  4 bytes. is 0x80 64 00 00 is 0x80 64 FF FF

Just convert the IP addreses to integers in hex and subtract them.  Convert the values to decimal before or after the subtraction to get the number of values in the range.

In your example, there are 0x00 00 FF FF values in the range (plus 1) for 65,536 values.

Good Luck,
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

The subnet mask for that range is 0x0000FFFF, so there are 2^16 (= 65536) addresses in the range.
>> The subnet mask for that range is 0x0000FFFF

I should have said 0xFFFF0000 of course ;)
Chris DentPowerShell DeveloperCommented:

Hey :)

There are a couple of ways.

Number of Hosts per Subnet = 2^(Number of Host Bits) - 2

In the example above you would have a mask of That means we have 16 bits available to Hosts (32 bits - 16 masked bits).

That means we can have this many addresses for hosts:

(2^16) - 2 = 65536 - 2 = 655534

Where the 2 is for the Network and Broadcast address which are unusable by hosts.


If you know your binary arithmetic and understand netmasks, then the answer should be pretty easy.

For your example, the third octet can have the values 0-255, and the fourth is the same.  There are 256*256 different combinations of these values, or 65536.  If the netmask is such that is at the beginning of the subnet range, then subtract 1.  If the netmask is such that is at the end of the subnet range, subtract 1.

So, if you're calculating the number of valid addresses in the subnet, there are 65534 valid IP addresses.
I should have said can't be used...
npinfotechAuthor Commented:
Great answers guys, but Uros' answer was more of what I was looking for.  
npinfotechAuthor Commented:
Thanks to everyone for responding to the post!  While all the answers were good, Uros had the method I was looking for.
>> Uros had the method I was looking for.

It's more complicated than the other suggested ways though ...
All Courses

From novice to tech pro — start learning today.