Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate number of hosts between two ips? c#

Tags:

c#

algorithm

ip

I have two ips:

1. 1.1.1.1
2. 4.4.4.4

obviously this is just an example, this is a dynamic calculator

how do i calculate number of hosts between said ips if subnet mask is irrelevant?

like image 236
Mickey Perlstein Avatar asked Nov 23 '25 02:11

Mickey Perlstein


1 Answers

To calculate the number of (theoretical) IP addresses you would convert each IP address to it's 32 bit integer format (which is actually what it really is), then it's just a matter of simple subtraction:

1.1.1.1 = 0x01010101 = 16843009
4.4.4.4 = 0x04040404 = 67372036

Number of addresses excluding the start and end address:

67372036 - 16843009 - 1 = 50529026

Number of addresses including the start and end address:

67372036 - 16843009 + 1 = 50529028

The number of actual usable addresses would be somewhat lower. Normally a few addresses in each C range is reserved for things like the gateway (router).

like image 197
Guffa Avatar answered Nov 25 '25 14:11

Guffa