Signed word lengths and indexes

Don nospam at nospam.com
Thu Jun 17 03:55:39 PDT 2010


Justin Spahr-Summers wrote:
> On Thu, 17 Jun 2010 10:00:24 +0200, Don <nospam at nospam.com> wrote:
>> (D has introduced ANOTHER instance of this with the ridiculous >>> 
>> operator.
>> byte b = -1;
>> byte c = b >>> 1;
>> Guess what c is!
>> )
>>
> 
> 127, right? I know at least RISC processors tend to have instructions 
> for both a logical and algebraic right shift. In that context, it makes 
> sense for a systems programming language.

Surprise! c == -1.
Because 1 is an int, b gets promoted to int before the shift happens.
Then the result is 0x7FFF_FFFF which then gets converted to byte, 
leaving 0xFF == -1.


More information about the Digitalmars-d mailing list