ip address math

Posted on 2007-11-26
Last Modified: 2010-08-05
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?  
Question by:npinfotech
  • 3
  • 3
  • 2
  • +3

Expert Comment

ID: 20350549
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
LVL 45

Expert Comment

ID: 20350558
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,
LVL 53

Expert Comment

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

Expert Comment

ID: 20350574
>> The subnet mask for that range is 0x0000FFFF

I should have said 0xFFFF0000 of course ;)
LVL 70

Expert Comment

by:Chris Dent
ID: 20350577

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.


Free camera licenses with purchase of My Cloud NAS

Milestone Arcus software is compatible with thousands of industry-leading cameras for added flexibility. Upon installation on your My Cloud NAS, you will receive two (2) camera licenses already enabled in the software. And for a limited time, get additional camera licenses FREE.

LVL 22

Expert Comment

ID: 20350594
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.

Accepted Solution

UrosVidojevic earned 500 total points
ID: 20350598
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...

Expert Comment

ID: 20350615
I should have said can't be used...

Author Closing Comment

ID: 31410993
Great answers guys, but Uros' answer was more of what I was looking for.  

Author Comment

ID: 20351113
Thanks to everyone for responding to the post!  While all the answers were good, Uros had the method I was looking for.
LVL 53

Expert Comment

ID: 20351431
>> Uros had the method I was looking for.

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
networking details on centos 6.6 4 53
Google Play app store not being blocked from smartphones 4 32
DNS and NSLOOKUP 21 57
DHCP Server Service stops on SBS 2011 3 33
Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
Lithium-ion batteries area cornerstone of today's portable electronic devices, and even though they are relied upon heavily, their chemistry and origin are not of common knowledge. This article is about a device on which every smartphone, laptop, an…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now