bitwize '&' operator trouble

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I ran across this problem with php 5.0.2 in Linux. This problem did not exist before and this code was used in php 5.0.0, 5.0.1, 5.0.2-dev, and several other cvs snaps along the way -all on Linux.

<?php
// example
// network IP and mask
$range = '192.168.0.0/24';

list($ip,$mask) = explode('/', $range);

// Calculate the number of hosts.
$numHosts = pow(2, (32 - $mask));

print "num hosts: $numHosts<br>\n";

// Netmask = fullmask - number of hosts.
$netmask = pow(2, 32) - $numHosts;

print "netmask: $netmask<br>\n";
print "human readable netmask: " . long2ip($netmask) . "<br>\n";

// address to number
$ipLong = ip2long($ip);

print "ip2long ip: $ipLong<br>\n";

// here is where the problem is:
print "(\$ipLong & \$netmask): ".($ipLong & $netmask)."<br>\n";
// PHP 5.0.2-dev (cli) (built: Sep 21 2004 14:39:26)
// prints out "-1062731776"
//PHP 5.0.2 (cli) (built: Sep 24 2004 13:58:57)
// prints out "1084751872"

// ...and so the following always fails:
// If the ip is not equal to (itself & netmask) it cannot be on the network boundary
if ($ipLong != ($ipLong & $netmask))
 echo "Ip NOT on network boundary!";
else
 echo "Ip IS on network boundary!";
?>

I'm sure there are other ways to test if an ip is on a the network boundary of a given CIDR and I'll appreciate any examples of that. However, I am more interested in why and'ing these two numbers returns a different value now. I'm unable to find any clues in the docs.

Thanks,

Jim Grill

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux