Re: Different binary results with different PHP versions



Yes, I have read the manual and notes below the manual. But actually, I
don't need exactly the decbin function. I have used that just to show
something going wrong. My point is the ^ operator. For ex, look at the
following code and its outputs:

echo (1 ^ -5817441119);
// Prints -1522473824 which is true result, at PHP 5.1.2 + Windows
// Prints -2147483647 which is false result, at PHP 4.4.2 + Linux

I also thought that the reason is that the number is too small, but why
PHP 5 calculates correctly? Is there a big difference in number types
between PHP 4 and PHP 5? I don't know if it helps but when I checked
PHP_INT_MAX, it is 2147483647 for both.

Erwin Moller wrote:
Skeleton wrote:

I am using Windows XP + PHP 5.1.2 at home for development and using
Linux + PHP 4.4.2 at server side. In my code, I am doing some bitwise
operations. While doing this, I have come accross to different results
from my functions that uses bitwise xor (^) operator. When I digg into
the code, I have tried to print out the binary of a decimal number. The
result was so:

echo decbin(-5817441119);
// Prints 10100101010000001110010010100001 at local (PHP 5.1.2)
// Prints 10000000000000000000000000000000 at server (PHP 4.4.2)

Is this a bug? How can I workaround this problem? Thanks.

Hi,

Did you read the Fine Manual?
http://nl2.php.net/manual/en/function.decbin.php

says:
string decbin ( int number )
----------------
Returns a string containing a binary representation of the given number
argument. The largest number that can be converted is 4294967295 in decimal
resulting to a string of 32 1's.
----------------

Your number is bigger, or actually smaller. ;-)

Read the usercontributed notes for details and solutions.

Regards,
Erwin Moller

.



Relevant Pages

  • Re: Object of class Person could not be converted to string
    ... PHP has been lousy at maintaining backward compatibility. ... Objects are always converted to the string "Object". ... there's no reasonable gain in usability by forcing it ...
    (comp.lang.php)
  • HOW TO: eval() into a string
    ... evaluate a string with embedded php into a string.) ... PHP Functions as Wiki-isms ... embedded PHP functions that generate strings into a string of pure ... Then we can capture what is in the output buffer ...
    (alt.php)
  • Re: Object of class Person could not be converted to string
    ... PHP has been lousy at maintaining backward compatibility. ... They wouldn't have if the language had been better planned from the start. ... Objects are always converted to the string "Object". ...
    (comp.lang.php)
  • Re: Wordpress Port
    ... database stuff in PHP ... I'm passing a number in as a string. ... achieved the dubious distinction of being the first language I actually ...
    (comp.lang.ruby)
  • Re: Object of class Person could not be converted to string
    ... Objects are always converted to the string "Object". ... But it's not unusual for doc to lag the actual code changes, - in PHP or any other product. ... typed language any more. ...
    (comp.lang.php)