|
Posted by Pedro Graca on 09/28/22 11:37
Palle Hansen wrote:
> yawnmoth wrote:
>> Using >> normally shifts bits to the right x number of times, where x
>> is specified by the programmer. As an example, 0x40000000 >> 8 yields
>> 0x00400000. This makes me wonder... why doesn't the same thing happen
>> with 0x80000000 >> 8? The number it yields is 0xff800000 - not
>> 0x00800000. The following script better demonstrates this:
>>
>> <?
>> echo sprintf('%08x',0x40000000 >> 8)."\n";
>> echo sprintf('%08x',0x80000000 >> 8);
>> ?>
>>
>> Anyway, any ideas?
>
> My guess is that PHP sees 0x8000000 as a *signed* integer
Seems like you're right, Palle.
I tested with this code:
<?php
header('Content-Type: text/plain');
$t = 0x80000000;
echo $t-1, '; ', $t, '; ', $t + 1, "\n";
var_dump($t);
/*********
* <quote src="http://www.php.net/unpack">
* CAUTION
* Note that PHP internally stores integral values as signed. If
* you unpack a large unsigned long and it is of the same size as
* PHP internally stored values the result will be a negative
* number even though unsigned unpacking was specified.
* </quote>
*********/
/* N: unsigned long (always 32 bit, big endian byte order) */
$unsignedlong = unpack('N', "\x80\x00\x00\x00");
var_dump($unsignedlong);
?>
--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Navigation:
[Reply to this message]
|