function calculateIPRange(ip, subnetMask) {
const ipBinary = ip.split('.').map(part => parseInt(part, 10).toString(2).padStart(8, '0')).join('');
const subnetBinary = subnetMask.split('.').map(part => parseInt(part, 10).toString(2).padStart(8, '0')).join('');
const networkBinary = ipBinary.split('').map((bit, index) => (bit === '1' && subnetBinary[index] === '1') ? '1' : '0').join('');
const networkAddress = networkBinary.match(/.{8}/g).map(byte => parseInt(byte, 2)).join('.');
const prefixLength = subnetBinary.indexOf('0');
const hostBits = 32 - prefixLength;
const endAddress = networkAddress.split('.').map(part => parseInt(part, 10)).reduce((result, octet, index) => {
if (index === 3) {
result.push(octet + Math.pow(2, hostBits) - 1);
} else {
result.push(octet);
}
return result;
}, []).join('.');
return `${networkAddress}/${prefixLength} - ${endAddress}/${prefixLength}`;
}
const ipRange = calculateIPRange('192.168.1.100', '255.255.255.0');
console.log(ipRange);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33