基于位运算符的IP和数值转换

直接上代码:

$str = '192.168.2.100';
echo 'IP:',$str,'<hr />';

$int = ip_long($str);
echo $int,'<hr />';

echo long_ip($int);

function ip_long($ip) {
    $ary = explode('.', $ip);
    return count($ary) == 4 ? $long = $ary[0] + ($ary[1] << 8) + ($ary[2] << 16) + ($ary[3] << 24) : 0;
}

function long_ip($int) {
    return $int > 255 ? ($int & 255).'.'.(($int >> 8) & 255).'.'.(($int >> 16) & 255).'.'.($int >> 24) : '';
}
原文地址:https://www.cnblogs.com/intval/p/3656804.html