473,386 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Xor bug in all PHP versions

I get two different results in doing a Xor on a large signed double
between linux servers.
working system is kernel 2.4 . bad results server is kernel 2.6.

PHP version does not matter. I compile PHP from 4.2.2 to latest.

test code:
<?PHP echo -4738698913 ^ 43814; ?>

results:

Good result from 2.4 kernel system is -443704711
Bad result from 2.6 kernel systems(tried 5 different systems) is
-2147439834

BUG in PHP or kernel?

Jan 18 '06 #1
12 2660
On 18 Jan 2006 13:32:23 -0800, "DigDug" <do*******@hotmail.com> wrote:
I get two different results in doing a Xor on a large signed double
between linux servers.
working system is kernel 2.4 . bad results server is kernel 2.6.

PHP version does not matter. I compile PHP from 4.2.2 to latest.

test code:
<?PHP echo -4738698913 ^ 43814; ?>

results:

Good result from 2.4 kernel system is -443704711
Bad result from 2.6 kernel systems(tried 5 different systems) is
-2147439834

BUG in PHP or kernel?


32 or 64 bit architecture?

Have you seen the related user notes on
http://www.php.net/manual/en/languag...rs.bitwise.php ? (Not all apply to
XOR but there are several in there that may interest you).

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Jan 18 '06 #2
DigDug wrote:
I get two different results in doing a Xor on a large signed double
between linux servers.
The ^ operator takes two integers, all bitwise operator do.
working system is kernel 2.4 . bad results server is kernel 2.6.

PHP version does not matter. I compile PHP from 4.2.2 to latest.

test code:
<?PHP echo -4738698913 ^ 43814; ?>
<?php if (PHP_VERSION>='4.4.0') echo PHP_INT_MAX; ?>

<?php echo var_dump(-4738698913); ?>

<?php echo -4738698913 | 0; ?>

<?php echo var_dump(-4738698913 | 0); ?>
results:

Good result from 2.4 kernel system is -443704711
64-bit machine?
Bad result from 2.6 kernel systems(tried 5 different systems) is
-2147439834
32-bit machine?
BUG in PHP or kernel?


Neither, I think.

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Jan 18 '06 #3
How is -4,738,698,913 ^ 43814 = -443,704,711 correct?

Jan 19 '06 #4
"Chung Leong" <ch***********@hotmail.com> wrote:

How is -4,738,698,913 ^ 43814 = -443,704,711 correct?


First, answer this question: how can you squeeze -4,738,698,913 into a
32-bit variable?

4,738,698,913 would be 1_1A72_CEA1 in hex, but that needs 33 bits. If you
truncate that to 32 bits, you get 1A72_CEA1. Xor that with 43,814, which
is AB26 in hex, and you get 1A72_6587, which just happens to be
443,704,711.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jan 20 '06 #5
Tim Roberts said the following on 20/01/2006 06:59:
"Chung Leong" <ch***********@hotmail.com> wrote:
How is -4,738,698,913 ^ 43814 = -443,704,711 correct?


First, answer this question: how can you squeeze -4,738,698,913 into a
32-bit variable?

4,738,698,913 would be 1_1A72_CEA1 in hex, but that needs 33 bits. If you
truncate that to 32 bits, you get 1A72_CEA1. Xor that with 43,814, which
is AB26 in hex, and you get 1A72_6587, which just happens to be
443,704,711.


But that's assuming that PHP performs XOR on the absolute values of the
operands, and then negates appropriately. i.e.:

(-4,738,698,913 ^ 43,814) == -(4,738,698,913 ^ 43,814)

which isn't the case, at least not always. e.g.:

echo (3 ^ 7) . "\n";
echo (-3 ^ 7) . "\n";

i.e. doing XOR directly on the 2's-complement representations of the
operands.

As a side-note:
I don't like the fact that even basic PHP code isn't portable between
platforms, i.e. it doesn't abstract away differences in platform bitwise
implementations of signed values.
--
Oli
Jan 20 '06 #6
Oli Filth wrote:
Tim Roberts said the following on 20/01/2006 06:59:
"Chung Leong" <ch***********@hotmail.com> wrote:
How is -4,738,698,913 ^ 43814 = -443,704,711 correct?

First, answer this question: how can you squeeze -4,738,698,913 into a
32-bit variable?

4,738,698,913 would be 1_1A72_CEA1 in hex, but that needs 33 bits. If
you
truncate that to 32 bits, you get 1A72_CEA1. Xor that with 43,814, which
is AB26 in hex, and you get 1A72_6587, which just happens to be
443,704,711.

But that's assuming that PHP performs XOR on the absolute values of the
operands, and then negates appropriately. i.e.:

(-4,738,698,913 ^ 43,814) == -(4,738,698,913 ^ 43,814)

which isn't the case, at least not always. e.g.:

echo (3 ^ 7) . "\n";
echo (-3 ^ 7) . "\n";

i.e. doing XOR directly on the 2's-complement representations of the
operands.

As a side-note:
I don't like the fact that even basic PHP code isn't portable between
platforms, i.e. it doesn't abstract away differences in platform bitwise
implementations of signed values.


No, Tim is correct. PHP CAN'T do an XOR on -4,738,698,913 on a 32 bit
system. The value doesn't fit into 32 bits!

It has nothing to do with the absolute value. In fact, in bit
operations other than right shift, there is no "absolute value". There
are just bits.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 20 '06 #7
Jerry Stuckle said the following on 20/01/2006 13:22:
Oli Filth wrote:
Tim Roberts said the following on 20/01/2006 06:59:
"Chung Leong" <ch***********@hotmail.com> wrote:

How is -4,738,698,913 ^ 43814 = -443,704,711 correct?
First, answer this question: how can you squeeze -4,738,698,913 into a
32-bit variable?

4,738,698,913 would be 1_1A72_CEA1 in hex, but that needs 33 bits.
If you
truncate that to 32 bits, you get 1A72_CEA1. Xor that with 43,814,
which
is AB26 in hex, and you get 1A72_6587, which just happens to be
443,704,711.

But that's assuming that PHP performs XOR on the absolute values of
the operands, and then negates appropriately. i.e.:

(-4,738,698,913 ^ 43,814) == -(4,738,698,913 ^ 43,814)

which isn't the case, at least not always. e.g.:

echo (3 ^ 7) . "\n";
echo (-3 ^ 7) . "\n";

i.e. doing XOR directly on the 2's-complement representations of the
operands.


No, Tim is correct. PHP CAN'T do an XOR on -4,738,698,913 on a 32 bit
system. The value doesn't fit into 32 bits!


I'm aware of that.

However, I now realise that -443,704,711 is the correct answer! Made a
mistake in my paper calculations! Apologies...


--
Oli
Jan 20 '06 #8
What founded this question is the following script.

http://www.googlecommunity.com/scrip...nk-source.phps

which works on some machines and not others. All my machines are 32bit.
From my tests I noticed the problem being with 2.6 kernel boxes and

not what bit the kernel/processor is.

Jan 23 '06 #9
How do you know the PHP lexical analyzer or parser is going to lop off a bit
for you? what if it does something else?
your number in hex is 0xFFFF FFFE E58D 315F ^ 0x0000 0000 0000 AB26=0xFFFF
FFFF 8000 AB26
2's complement numbers in hex are represented differently in binary...
this is from pcalc, a program from analogx.com (64-bit programmer's
calculator)

"Tim Roberts" <ti**@probo.com> wrote in message
news:39********************************@4ax.com...
"Chung Leong" <ch***********@hotmail.com> wrote:

How is -4,738,698,913 ^ 43814 = -443,704,711 correct?


First, answer this question: how can you squeeze -4,738,698,913 into a
32-bit variable?

4,738,698,913 would be 1_1A72_CEA1 in hex, but that needs 33 bits. If you
truncate that to 32 bits, you get 1A72_CEA1. Xor that with 43,814, which
is AB26 in hex, and you get 1A72_6587, which just happens to be
443,704,711.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.

Jan 24 '06 #10

"DigDug" <do*******@hotmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
I get two different results in doing a Xor on a large signed double
between linux servers.
working system is kernel 2.4 . bad results server is kernel 2.6.

PHP version does not matter. I compile PHP from 4.2.2 to latest.

test code:
<?PHP echo -4738698913 ^ 43814; ?>

results:

Good result from 2.4 kernel system is -443704711
Bad result from 2.6 kernel systems(tried 5 different systems) is
-2147439834

2.6 kernel system is at least a 64-bit base. it's not a bug.
-4738698913=0xFFFF FFFE E58D 315F
43814=0x0000 0000 0000 AB26
-2147439834=0xFFFF FFFF 8000 AB26
that last number is what I get for a result from a 64-bit calculator.

When a C or C++ program like PHP is compiled on a 64-bit processor or using
flags for 64-bit int's, you will probably get this. The size of an int will
become 64 bits, not 32. similarly, if you are compiling on a 16-bit
processor and no 32-bit registers are available, you may get 16-bit ints.
The manual doesn't seem to take this into account. possibly 64-bit
processors were not supported until later versions of gcc and/or kernel? or
possibly (more likely) the default settings for the processor are stored in
the kernel.
BUG in PHP or kernel?

Jan 24 '06 #11
"Jim Michaels" <jm******@yahoo.com> wrote:

How do you know the PHP lexical analyzer or parser is going to lop off a bit
for you? what if it does something else?
PHP does not handle integers larger than "long" on its machine. Thus, on a
32-bit machine, I KNOW it's going to lop off a bit.
your number in hex is 0xFFFF FFFE E58D 315F ^ 0x0000 0000 0000 AB26=0xFFFF
FFFF 8000 AB26
No, that would be my number in a 64-bit variable. My description below
specifically mentions a 32-bit variable, as the OP was using.
2's complement numbers in hex are represented differently in binary...
No, they aren't. The numbers are ALL binary. Whether I print them in
binary or hex or decimal is irrelevant to the way they are processed.
However, the SIZE of the variable is important.
this is from pcalc, a program from analogx.com (64-bit programmer's
calculator)
Right. That's the result you'd get from a 64-bit processor. PHP on a
32-bit processor will produce the result I described.
"Tim Roberts" <ti**@probo.com> wrote in message:
"Chung Leong" <ch***********@hotmail.com> wrote:

How is -4,738,698,913 ^ 43814 = -443,704,711 correct?


First, answer this question: how can you squeeze -4,738,698,913 into a
32-bit variable?

4,738,698,913 would be 1_1A72_CEA1 in hex, but that needs 33 bits. If you
truncate that to 32 bits, you get 1A72_CEA1. Xor that with 43,814, which
is AB26 in hex, and you get 1A72_6587, which just happens to be
443,704,711.

--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jan 24 '06 #12
Oli Filth wrote:
As a side-note:
I don't like the fact that even basic PHP code isn't portable between
platforms, i.e. it doesn't abstract away differences in platform bitwise
implementations of signed values.


I don't really see why the operation would be platform dependent. If I
remember correctly, a long is defined as 32 bit. Int is the one that
varies between architectures. In PHP, a convert from double to integer
is done by the DVAL_TO_LVAL macro, which is defined as:

(l) = (d) > LONG_MAX ? (unsigned long) (d) : (long) (d)

A cast to long should be the same even on a 64 bit OS/CPU.

I wonder if it's a difference in the compiler or C library. Typically a
cast from a float to a long is handled with a call to ftol(). Maybe the
overflow behavior is different.

Jan 24 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ted | last post by:
I would like to collect opinions on which versions of Python should be considered watershed versions. By this I mean versions which are stable and contain significant landmark features. As an...
3
by: zorro | last post by:
Hello, If I have to test my code on different Netscape versions, can I download and run those versions on the same computer or will it create conflicts? And if I had to test only one version...
0
by: Oliver Elphick | last post by:
The attached proposal is written primarily for Debian. Its motivation is that the current package upgrade process is pretty flaky and also that the current packaging does not really provide for...
14
by: wolftor | last post by:
1) Is there a free runtime version of Access available that is more recent than the one for Access 2000? 2) If I create an application (MDE) in A2K, will it run on all later versions of Access?...
4
by: Dalan | last post by:
After reading and experiencing the phenomenon of installing MS Office 2000 on a system that already has MS Office 97, or for that matter just Access 97 Runtime, I saw the ugliness that ensues. If...
5
by: Michael Maes | last post by:
Hi, We have an ERP-Application that needs to interact with an "external accountancy program". This is acchieved through a "Connector" (ActiveX-dll) so kindly provided by the Accountancy-Program....
5
by: Laurence | last post by:
In VS.2005 using VB.NET There are two versions on every project, The Assembly Version and the File Version. Why are there two different versions? As far as I can tell, there is not need for...
37
by: Allen Browne | last post by:
If you develop for others, you probably have multiple versions of Access installed so you can edit and create MDEs for clients in different versions. This works fine under Windows XP, even with...
9
by: python | last post by:
Background: I'm going to be processing some raw transaction logs that are 30G in size. As part of this processing I may need to create some very large dictionary structures. I will be running my...
1
by: M.-A. Lemburg | last post by:
On 2008-07-25 08:13, python@bdurham.com wrote: Yes. But then Intel Itanium is being phased out anyway and the AMD64 build works on both Intel and AMD processors. True.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.