Page 1 of 1

Is It PHP's Bug?

Posted: Thu Aug 09, 2007 5:13 am
by devmonirul
Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


In java the following code generates the following output. But when i converted this code to PHP, the output was not same. Even i tried with 64 bit  bitwise operation. Anybody can convert this snippet to PHP so that the same result come?

[syntax="java"]long a = 0;
long c = -589949350;

for(int j=0;j<4;j++)
{
  a=(a^c&0xff)*0x9e3779b1;  
  c=c>>8; 
  println(a);
  println(c);
}
===========================
Output
===========================[/syntax]

Code: Select all

Loop 1:
-------
a = -147647838150
c = -2304490

Loop 2:
-------
a = 2413261624394330732
c = -9002

Loop 3:
-------
a = 4360021387243289754
c = -36

Loop 4:
-------
a = -5030538659600629146
c = -1

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Aug 09, 2007 5:23 am
by s.dot

Code: Select all

$a = 0;
$c = -589949350;
for($j=0;$j<4;$j++)
{
    $a = ($a^$c&0xff)*0x9e3779b1;
    $c = $c>>8;
    echo $a . '<br />' .  $c . '<br /><br />';
}

Code: Select all

238899218490
-2304490

-4.2973990407145E+018
-9002

3.5674548642156E+018
-36

2.7526387461445E+018
-1
Looks like $c is always right. Not sure where $a is going wrong.

Posted: Thu Aug 09, 2007 5:31 am
by devmonirul
Yes, $c is always right but $a is not. What can I do now. Please help me.

Posted: Thu Aug 09, 2007 7:34 am
by timvw
(As java Long is 64 bits...) How can you achieve 64bits bitwise operation with php on a 32bits system? I can't find it in the manual under http://be.php.net/language.operators.bitwise

Posted: Thu Aug 09, 2007 8:11 am
by devmonirul
Finally I'm successful. FYI, all the operation (^, & and *) need to be done in binary mode and obviously in 64 bit.

@ timvw: Yes, php do not support 64 bit. But still you can do it;