-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipbetween.js
More file actions
24 lines (18 loc) · 809 Bytes
/
ipbetween.js
File metadata and controls
24 lines (18 loc) · 809 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Write a function that accepts a starting and ending IPv4 address,
// and returns the number of IP addresses from start to end,
// excluding the end IP address. All input to the ipsBetween function
// will be valid IPv4 addresses in the form of strings. The ending address will
// be at least one address higher than the starting address.
// Examples:
// ipsBetween("10.0.0.0", "10.0.0.50") => returns 50
// ipsBetween("10.0.0.0", "10.0.1.0") => returns 256
// ipsBetween("20.0.0.10", "20.0.1.0") => returns 246
function ipsBetween(start, end){
return ipToInt32(end) - ipToInt32(start);
}
function ipToInt32(ip) {
return parseInt(ip.split('.').map(function(v) {
var bin = parseInt(v).toString(2);
return new Array(9 - bin.length).join('0') + bin;
}).join(''), 2);
}