iasksillyquestions
iasksillyquestions

Reputation: 5689

Calculating the IP after the nth range

I have an IP address 10.0.2.0

The next IP after a block of 64 (10.0.2.0 to 10.0.2.63) is 10.0.2.64 After that (10.0.2.64 to 10.0.2.127 ) 10.0.2.128 etc

How do I calculate the nth?

I had assumed roughly

a = (n*64) mod 256 b = 255/n

10.0.2+b.a

Upvotes: 0

Views: 236

Answers (1)

iasksillyquestions
iasksillyquestions

Reputation: 5689

Here's the final solution (in JavaScript):

function incrementIp(ip,nips){
  var input = ip.split(".");
  var ip = (input[0] << 24) | (input[1] << 16) | (input[2] << 8) | (input[3] << 0);
  ip+=nips; 
  return (ip>>24 & 0xff )+ "." + (ip>>16 &0xff) + "." +( ip>>8 &0xff) + "." + (ip & 0xff);
}

Upvotes: 1

Related Questions