I need to calculate the netmask given a Start and End ip address for a block in a subnet, in javascript. I leveraged this answer https://stackoverflow.com/a/8872819/664479 and
With a startAddress of ac164980
and endAddress of ac16498e
var scope = ipScope;
var s = parseInt("0x"+startAddress ,16);
var e = parseInt("0x"+endAddress ,16);
var m = parseInt("0xFFFFFFFF",16);
var nm = ""+(m ^ s ^ e);
I expected FFFFFFC0
but got -15
Where'd I go wrong?
There are actually 2 problems here. The first is the calculation assumption using startIP and endIP.
It really should be the scopeSize
of the subnet that the startIP and endIP lie within.
The second is the representation of the negative value that's returned. That's Fixed with:
var nm = (0xFFFFFFFF + (-1 ^(scope-1)) +1).toString(16).toUpperCase();