PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Problems using bitwise &
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Problems using bitwise &

Réponse
 
LinkBack Outils de la discussion
Vieux 28/03/2008, 12h15   #1
Philluminati
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Problems using bitwise &


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
please?
  Réponse avec citation
Vieux 28/03/2008, 13h27   #2
Philluminati
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems using bitwise &

On Mar 28, 11:15 am, Philluminati <Phillip.Ross.Tay...@gmail.com>
wrote:
> 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
> please?


My current very-hacky workaround is to do this:

$res = mysql_query("SELECT $ip & $subnet;");

....

but as you can see it's bordering on "very wrong"!
  Réponse avec citation
Vieux 28/03/2008, 14h30   #3
Alexey Kulentsov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems using bitwise &

Philluminati wrote:
>
> $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'];


>
> 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);
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h15.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10487 seconds with 11 queries