How can I work out how many IP addresses there are in a given range?

I’m wondering if there is a formula to work out the amount of IP addresses in a given range.

I have a system that takes either a single IP address or a start and and end IP address:

  • A single one might be: 145.16.23.241
  • A range might be: 145.16.23.122 - 145.16.23.144

The people inputting these aren’t technical, nor are they receiving these details from technical people, so suggesting using CIDR notation isn’t an answer for me.

I want to be able to highlight that when entering a range such as: 122.100.10.12 - 128.10.200.140 it might not actually be a range and rather 2 singular IP addresses since such a range would comprise of x (large number of) IP addresses.

Is there a basic formula to help me highlight this?

2

IP addresses are a 32 bit integer which we typically express as four octets for human purposes. You could get the number of addresses in a range by turning both into their 32 bit integer representations and subtracting.

2

You won’t be able to get an accurate count without any information about whether the range crosses subnet boundaries and what those subnets are. For example, 10.10.7.0 - 10.10.8.255 is a contiguous range of 512 addresses in the context of a /16. If that range covers two /24 blocks, the count is 508 because each block has a network address and a broadcast address.

Making sure the entered upper bound is greater than or equal to the lower bound and using subtraction to measure the distance between them is good for a quick sanity check. You’ll need to decide what’s a reasonable difference before you raise the red flag.

After you’ve converted each octet to an 8-bit integer, converting the lot of them to a single 32-bit integer is a matter of doing this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>(octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4
</code>
<code>(octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4 </code>
(octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4

3

You have to be clear how you are defining these ranges. Maybe you meant all of the addresses whose 32-bit representations fall between the two numbers, and that’s what the other answers here seem to assume. So for example, 1.0.0.0 - 1.0.2.0 includes all of the following IP addresses:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>1.0.0.0 - 1.0.0.255
1.0.1.0 - 1.0.1.255
1.0.2.0
</code>
<code>1.0.0.0 - 1.0.0.255 1.0.1.0 - 1.0.1.255 1.0.2.0 </code>
1.0.0.0 - 1.0.0.255
1.0.1.0 - 1.0.1.255
1.0.2.0

However, when I first read your question, what I thought you meant was defining separate ranges for each byte, in which case the range 1.0.0.0 - 1.0.2.0 would mean only the following three addresses:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>1.0.0.0
1.0.1.0
1.0.2.0
</code>
<code>1.0.0.0 1.0.1.0 1.0.2.0 </code>
1.0.0.0
1.0.1.0
1.0.2.0

For the first case, as others stated the correct answer is

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>abs(ip2-ip1)+1
</code>
<code>abs(ip2-ip1)+1 </code>
abs(ip2-ip1)+1 

But in the second case, the correct answer would be

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>product(abs(ip2[i] - ip1[i]) + 1)
</code>
<code>product(abs(ip2[i] - ip1[i]) + 1) </code>
product(abs(ip2[i] - ip1[i]) + 1) 

where [i] means the ith byte of that IP, and the product is taken over the four bytes.

Make sure it’s clear to your users which of these you mean.

Note that to properly convert dotted IP addresses to a 32-bit number and do mathy things on them reliably, you need to use an unsigned integer type. If the only integer types available to you are signed, then…

Another way to calculate it is this: starting with the first octet that is different, multiply each octet’s difference from that point on by the following numbers, and then add these differences together:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>1: 16777216
2: 65536
3: 256
4: 1
</code>
<code>1: 16777216 2: 65536 3: 256 4: 1 </code>
1: 16777216
2: 65536
3: 256
4: 1

For example, using your range 122.100.10.12 – 128.10.200.140:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 128 10 200 140
- 122 100 10 12
= 6 -90 190 128
= 16777216 * 6
+ 65536 * -90
+ 256 * 190
+ 1 * 128
= 94813824
</code>
<code> 128 10 200 140 - 122 100 10 12 = 6 -90 190 128 = 16777216 * 6 + 65536 * -90 + 256 * 190 + 1 * 128 = 94813824 </code>
  128  10   200  140
- 122  100   10   12
=   6 -90   190  128

= 16777216 * 6 
+ 65536 * -90
+ 256 * 190
+ 1 * 128
= 94813824

This is the same value you’d get doing the whole conversion of

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>0x800AC88C = 2148190348
0x7A640A0C = 2053376524
</code>
<code>0x800AC88C = 2148190348 0x7A640A0C = 2053376524 </code>
0x800AC88C = 2148190348
0x7A640A0C = 2053376524

and then subtracting.

Additionally, you can also perform the same operation this way:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>((a[0] - b[0]) * 256 + a[1] - b[1]) * 256 + a[2] - b[2]) * 256 + a[3] - b[3]
</code>
<code>((a[0] - b[0]) * 256 + a[1] - b[1]) * 256 + a[2] - b[2]) * 256 + a[3] - b[3] </code>
((a[0] - b[0]) * 256 + a[1] - b[1]) * 256 + a[2] - b[2]) * 256 + a[3] - b[3]

This is easily susceptible to a reduce that collapses each next pair of values repeatedly.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật