You are here: Re: confusing >> results « PHP Programming Language « IT news, forums, messages
Re: confusing >> results

Posted by Pedro Graca on 09/27/22 11:37

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.

--
If you're posting through Google read <http://cfaj.freeshell.org/google>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация