IP and Subnet to Start IP End IP

javascript, php

Solution

I will assume you will also need for other mask like 8,16,...

ip="8.8.8.0/24"

extract each parts `ip_array=ip.match(/(\d+)\.(\d+)\.(\d+)\.(\d+)\/(\d+)/)` //js regex

convert to number `ip_num = (ip[1]<<24)+(ip[2]<<16)+(ip[3]<<8)+(+ip[4])` //# 0x08080800

`mask=(1<<(32-ip[5]))-1` //# 0xFF

`ip_num | mask` will be 0x080808FF which is 8.8.8.255

`ip_num & (0xffffffff ^ mask)` will be 0x08080800 which is 8.8.8.0

you need to convert `ip_num` back to ip string back

Problem

So I have an IP with a Subnet like: 8.8.8.0/24 How can I convert this to 8.8.8.0 and 8.8.8.255 (actually their ip2long resultants) In PHP and JavaScript

Original source