|
Posted by Jerry Stuckle on 09/27/32 11:37
Pedro Graca wrote:
> Jerry Stuckle wrote:
>
>>Pedro Graca wrote:
>>
>>>Let's say -8 in binary is (ignore the spaces)
>>>MSB == 10000000 00000000 00000000 00001000 == LSB
>>>
>>>if you shift this right without concern for the sign, you get
>>>MSB == 00100000 00000000 00000000 00000010 == LSB
>>>
>>>which is 536870914
>>>
>>>
>>>Do you think -8 >> 2 (-8 divided by 4) should equal 536870914? :)
>>>
>>
>>But -8 binary is:
>>
>> 11111111 11111111 11111111 11111000
>
>
> Indeed it is (on my machines).
> However some other machine running PHP could use sign-and-magnitude
> representation of negative numbers instead of two's complement.
> For that machine my reasoning above stands :-)
>
> My book on C (The C Programming Language, Kernighan & Ritchie) says this
> about >>
>
> <quote>
> Right shifting a signed quantity will fill with sign bits
> ("arithmetic shift") on some machines and with 0-bits
> ("logical shift") on others.
> </quote>
>
> So, taking into account that PHP is written in C, I guess the effect of
> 0x80000000 >> 8
> is implementation defined (can be 0xff800000 on some (most) machines
> and 0x00800000 on other machines)
>
> And as PHP does not have C's `unsigned long int` it's best to avoid
> right shifting negative values.
>
Pedro,
Just because it's undefined in C doesn't mean it has to be in PHP.
Different languages can have different rules.
And everything is eventually built on machine language anyway.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|