Connecting Tech Pros Worldwide Help | Site Map

Problems using bitwise &

  #1  
Old March 28th, 2008, 12:25 PM
Philluminati
Guest
 
Posts: n/a

I'm trying to perform some basic mathematics on IP Addresses. IP's are
normally represented as 32 bit unsigned integers. However I am getting
strange results when using the operators in PHP.

If you run this is a mysql query browser: select (3388826209 &
4294867288) as val; it will tell you the correct answer is:
3388760128. This is the answer I want. My snippet of PHP code is here:

$ip = $networkObjectRow['sipv4addr'];
$subnet = $networkObjectRow['eipv4addr'];

//make php treats these likes numbers and not strings

$ip += (int)0;
$subnet += (int)0;

if ($ip == 0)
return "NOT SET";

$ans = $ip & $subnet; //Bitwise AND.

print("<!-- sip F (ip=$ip,subnet=$subnet) returned is
$ans -->\n");
return ($ans);

However my output is like this:

<!-- sip F (ip=170419457,subnet=4294967040) returned is 170419456 -->
<!-- sip F (ip=170419457,subnet=4294967040) returned is 170419456 -->
<!-- sip F (ip=3388826225,subnet=4294967280) returned is -906141072 --
Quote:
>
<!-- sip F (ip=3388826225,subnet=4294967280) returned is -906141072 --
Quote:
>
<!-- sip F (ip=170419969,subnet=4294967040) returned is 170419968 -->

<!-- sip F (ip=170419969,subnet=4294967040) returned is 170419968 -->

Where the IP starts 17... the answer is correct. When the value starts
3388 the value is wrong.

I guess this is because PHP is treating the number as a 64 bit one
rather than an unsigned 32 bit one but I'm not really sure. Can anyone
help please?
  #2  
Old March 28th, 2008, 02:35 PM
Alexey Kulentsov
Guest
 
Posts: n/a

re: Problems using bitwise &


Philluminati wrote:
Quote:
>
$ip = $networkObjectRow['sipv4addr'];
$subnet = $networkObjectRow['eipv4addr'];
>
//make php treats these likes numbers and not strings
>
$ip += (int)0;
$subnet += (int)0;
$ip = (int)$networkObjectRow['sipv4addr'];
$subnet = (int)$networkObjectRow['eipv4addr'];

Quote:
>
print("<!-- sip F (ip=$ip,subnet=$subnet) returned is
$ans -->\n");
return ($ans);
>
However my output is like this:
>
<!-- sip F (ip=170419457,subnet=4294967040) returned is 170419456 -->
<!-- sip F (ip=170419457,subnet=4294967040) returned is 170419456 -->
<!-- sip F (ip=3388826225,subnet=4294967280) returned is -906141072 --
<!-- sip F (ip=3388826225,subnet=4294967280) returned is -906141072 --
<!-- sip F (ip=170419969,subnet=4294967040) returned is 170419968 -->
>
<!-- sip F (ip=170419969,subnet=4294967040) returned is 170419968 -->
>
Where the IP starts 17... the answer is correct. When the value starts
3388 the value is wrong.
PHP _outputs_ it as signed integer. Try formatted output to clear
this question:

printf("<!-- sip F (ip=%08X,subnet=%08X) returned is %08X
-->\n",$ip,$subnet,$ans);
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
"error_reporting" setting not being recognized in my php.ini file laredotornado@zipmail.com answers 1 October 3rd, 2006 03:25 PM
Cannot use mail() in IE, only works in a debugger--help baustin75@gmail.com answers 8 October 5th, 2005 06:15 PM
Trouble with system() function Penn Markham answers 9 July 17th, 2005 05:43 AM
$_SERVER returns empty value tornado answers 3 July 17th, 2005 03:27 AM