Problems using bitwise &
- From: Philluminati <Phillip.Ross.Taylor@xxxxxxxxx>
- Date: Fri, 28 Mar 2008 04:15:25 -0700 (PDT)
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 --
<!-- 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.
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?
.
- Follow-Ups:
- Re: Problems using bitwise &
- From: Alexey Kulentsov
- Re: Problems using bitwise &
- From: Philluminati
- Re: Problems using bitwise &
- Prev by Date: Re: RSS Feed Creation Question
- Next by Date: Re: PDO/MYSQL Problem
- Previous by thread: Need help with PHP + MySQL on W2k
- Next by thread: Re: Problems using bitwise &
- Index(es):
Relevant Pages
|