Signed word lengths and indexes

Walter Bright newshound1 at digitalmars.com
Thu Jun 17 14:55:00 PDT 2010


Andrei Alexandrescu wrote:
> Walter Bright wrote:
>> Steven Schveighoffer wrote:
>>> On Thu, 17 Jun 2010 15:24:52 -0400, Don <nospam at nospam.com> wrote:
>>>
>>>>
>>>> A question I have though is, Java has >>>. Does Java have these 
>>>> problems too?
>>>
>>> Java doesn't have unsigned values, so it's necessary to use regular 
>>> int's as bitmasks, hence the extra operator.
>>
>> The reason D has >>> is to cause an unsigned right shift to be 
>> generated without needing to resort to casts as one has to in C.
>>
>> The problem with such casts is they wreck generic code.
> 
> No.
> 
> http://www.digitalmars.com/d/2.0/phobos/std_traits.html#Unsigned
> 
> void fun(T)(T num) if (isIntegral!T)
> {
>     auto x = cast(Unsigned!T) num;
>     ...
> }

It's not a perfect replacement, as in if T is a custom integer type, you have to 
extend the template to support it. Furthermore, now your BigInt custom type also 
has to support a cast to unsigned just so it can right shift. Also, T may not be 
readily identifiable, so you'd have to write:

     cast(Unsigned!(typeof(expr)) expr;


More information about the Digitalmars-d mailing list