|
Posted by Jerry Stuckle on 01/20/06 15:22
Oli Filth wrote:
> Tim Roberts said the following on 20/01/2006 06:59:
>
>> "Chung Leong" <chernyshevsky@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.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|