Is It PHP's Bug?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
devmonirul
Forum Newbie
Posts: 3
Joined: Thu Aug 09, 2007 5:06 am

Is It PHP's Bug?

Post 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]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
devmonirul
Forum Newbie
Posts: 3
Joined: Thu Aug 09, 2007 5:06 am

Post by devmonirul »

Yes, $c is always right but $a is not. What can I do now. Please help me.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
devmonirul
Forum Newbie
Posts: 3
Joined: Thu Aug 09, 2007 5:06 am

Post 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;
Post Reply