hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : PHP 5 xor bug
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

PHP 5 xor bug

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 05-07-2007, 04:05 PM
liandra liandra is offline
WHT Addict
 
Join Date: Jul 2006
Location: Lorem ipsum dolor sit ame
Posts: 161

PHP 5 xor bug


PHP 5 has a really annoying bug in processing xor (^) with (-) value variable.

Code:
<?php

$a=-5799680607;
$b=167160;

echo $a ^ $b;
?>
on PHP 4 the result is : -1504876199
on PHP 5 the result is : -2147316488


Tested on 2 different PHP 5 box (debian and Fedora)

I found this bug report on PHP site:
http://bugs.php.net/bug.php?id=41237

but it seems they haven't resolve it yet...
http://www.php.net/ChangeLog-5.php#5.2.2

the latest bugs they fixed was #41215

anybody know a workaround this bug?

__________________
■ Need an eNom retail/reseller account? PM me

Reply With Quote


Sponsored Links
  #2  
Old 05-11-2007, 01:08 AM
tobiasly tobiasly is offline
Aspiring Evangelist
 
Join Date: Dec 2005
Posts: 368
This isn't a bug, both your answers are wrong.

The problem is that, on a 32-bit machine, it is impossible to represent the number -5799680607 (your value of $a) in binary, so a true xor() can't be performed. To test this, run the following code on each machine:

PHP Code:
<?php
echo decbin(-5799680607);
echo(
"\n");
?>
Each one may give you a different answer, but they're both wrong if you're on a 32 bit machine, because the correct answer is:
1111111111111111111111111111111010100110010011111110010110100001

See http://en.wikipedia.org/wiki/Twos_complement for how negative numbers are represented in binary. So, here are the "correct" representations for your values of $a, $b, and the correct result, which is -5799843495:

1111111111111111111111111111111010100110010011111110010110100001
0000000000000000000000000000000000000000000000101000110011111000
1111111111111111111111111111111010100110010011010110100101011001

If you can keep your eyeballs in line, you will see that the bottom result is correct, with each bit the xor() of the above two. And if you run your test script on a 64-bit machine, you will get the correct result.

So I'm guessing they changed the way that PHP handles integers in an overflow condition, but it doesn't matter because neither is correct. So, what is it you're actually trying to achieve here? I'm guessing you're just trying to get some 3rd party code working the same between versions, instead of actually doing some sort of binary math?

Reply With Quote
  #3  
Old 05-11-2007, 01:22 AM
liandra liandra is offline
WHT Addict
 
Join Date: Jul 2006
Location: Lorem ipsum dolor sit ame
Posts: 161
Quote:
Originally Posted by tobiasly View Post
This isn't a bug, both your answers are wrong.

The problem is that, on a 32-bit machine, it is impossible to represent the number -5799680607 (your value of $a) in binary, so a true xor() can't be performed. To test this, run the following code on each machine:

PHP Code:
<?php
echo decbin(-5799680607);
echo(
"\n");
?>
Each one may give you a different answer, but they're both wrong if you're on a 32 bit machine, because the correct answer is:
1111111111111111111111111111111010100110010011111110010110100001

See http://en.wikipedia.org/wiki/Twos_complement for how negative numbers are represented in binary. So, here are the "correct" representations for your values of $a, $b, and the correct result, which is -5799843495:

1111111111111111111111111111111010100110010011111110010110100001
0000000000000000000000000000000000000000000000101000110011111000
1111111111111111111111111111111010100110010011010110100101011001

If you can keep your eyeballs in line, you will see that the bottom result is correct, with each bit the xor() of the above two. And if you run your test script on a 64-bit machine, you will get the correct result.

So I'm guessing they changed the way that PHP handles integers in an overflow condition, but it doesn't matter because neither is correct. So, what is it you're actually trying to achieve here? I'm guessing you're just trying to get some 3rd party code working the same between versions, instead of actually doing some sort of binary math?

decbin result :
on PHP 5 :

tested on 2 boxes (a Turion 64 and a Pentium 4 2.4 Ghz - 32 bit)
result is :

echo decbin (-5799680607) => 10000000000000000000000000000000

on PHP 4 (Tested on 32 bit machine - Intel(R) Pentium(R) 4 CPU 3.00GHz):

echo decbin (-5799680607) => 10100110010011111110010110100001

your result 1111111111111111111111111111111010100110010011111110010110100001
is 64 bits


The script is running fine on a PHP 4 environment, but not in PHP 5.

__________________
■ Need an eNom retail/reseller account? PM me

Reply With Quote
Sponsored Links
  #4  
Old 05-11-2007, 01:24 AM
liandra liandra is offline
WHT Addict
 
Join Date: Jul 2006
Location: Lorem ipsum dolor sit ame
Posts: 161
I tried it again on Intel(R) Xeon(TM) CPU 3.00GHz PHP 5 result is the same 1 followed by 31 zeros

__________________
■ Need an eNom retail/reseller account? PM me

Reply With Quote
  #5  
Old 05-11-2007, 01:36 AM
tobiasly tobiasly is offline
Aspiring Evangelist
 
Join Date: Dec 2005
Posts: 368
Quote:
Originally Posted by liandra View Post
decbin result :
on PHP 5 :

tested on 2 boxes (a Turion 64 and a Pentium 4 2.4 Ghz - 32 bit)
result is :

echo decbin (-5799680607) => 10000000000000000000000000000000

on PHP 4 (Tested on 32 bit machine - Intel(R) Pentium(R) 4 CPU 3.00GHz):

echo decbin (-5799680607) => 10100110010011111110010110100001
Again, both these results are wrong. Even the PHP 4 value is wrong. It's just wrong in a different way. If you test it on a 64-bit OS, you will get the same answer I got, because that's the only correct answer.

So, don't try to "fix" the problem or find a "workaround" because there is none. This will not be "fixed" in a future PHP release, and the bug report you found will not be fixed because the bug itself is invalid. The problem is that you're trying to perform an operation that your OS can't handle. You need to modify the script to test for an overflow condition and throw an error instead.

Reply With Quote
  #6  
Old 05-11-2007, 01:59 AM
liandra liandra is offline
WHT Addict
 
Join Date: Jul 2006
Location: Lorem ipsum dolor sit ame
Posts: 161
you're right, all my os is running 32 bit kernel even in the turion 64 machine. So, I guess what you're saying about how PHP 5 handle overflow is right.

I'm trying to install Google Page Rank script which required some checksum calculation, and it always works in PHP 4, even though it cut the half left 32 bits.

__________________
■ Need an eNom retail/reseller account? PM me

Reply With Quote
  #7  
Old 05-11-2007, 05:10 AM
liandra liandra is offline
WHT Addict
 
Join Date: Jul 2006
Location: Lorem ipsum dolor sit ame
Posts: 161
I think I found out the solution by using GMP function.

Code:
$a=gmp_init ('-5799680607',10);
$b=gmp_init ('167160',10);

echo gmp_strval (gmp_xor ($a, $b),10);
the result is correct : -5799843495

__________________
■ Need an eNom retail/reseller account? PM me

Reply With Quote
  #8  
Old 05-11-2007, 11:31 AM
tobiasly tobiasly is offline
Aspiring Evangelist
 
Join Date: Dec 2005
Posts: 368
Quote:
Originally Posted by liandra View Post
I think I found out the solution by using GMP function.

Code:
$a=gmp_init ('-5799680607',10);
$b=gmp_init ('167160',10);

echo gmp_strval (gmp_xor ($a, $b),10);
the result is correct : -5799843495
Great! I didn't even know about the GMP library, that's good to know for future reference.

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?