D arithmetic problem

Don nospam at nospam.com
Wed Jun 3 21:06:33 PDT 2009


Steven Schveighoffer wrote:
> On Wed, 03 Jun 2009 02:20:33 -0400, Tim Matthews 
> <tim.matthews7 at gmail.com> wrote:
> 
>> Paul D. Anderson wrote:
>>> The behavior is consistent with the specification -- see 
>>> http://www.digitalmars.com/d/1.0/type.html.
>>>  Most C-based languages will do the same.
>>>
>>
>> You are slightly missing the point. The point is byte should be 
>> unsigned and a separate name for signed. Dot net actually has Byte for 
>> unsigned and SByte for signed so its harder to make that error. Too 
>> late to change now though.
> 
> Count me as one of the people who sees byte as signed.  In fact, C# 
> confused me when I wanted to use an unsigned byte, and couldn't find 
> ubyte.  I think it all depends on what you learned first.
> 
> Making byte unsigned and introducing sbyte would go against the current 
> convention of "without a u is signed, with a u is unsigned".

Yes, but we need to recognize that that convention is not intuitive when 
applied to 'byte'. The integral types originate in C, and they are 
abbreviations.

short -> short int
int   ->   int
long  -> long int

In mathematical usage, an 'integer' is signed. So the 'u' prefix makes 
sense. However, 'byte' is not 'byte int'. And the word 'byte' does NOT 
have an implied signed-ness in popular usage (in fact, it doesn't have 
terribly much implied connotation of being a number, merely a set of 8 
bits).

> BTW, with C#, the following code results in an error:
> 
>         static void Main(string[] args)
>         {
>             uint v;
>             v = 31234;
>             sbyte t = -1;
>             v |= t;
>             Console.Out.WriteLine(v);
>         }
> 
> Eror: Cannot implicitly convert type 'sbyte' to 'uint'. An explicit 
> conversion exists (are you missing a cast?)
> 
> I would be in favor of a similar behavior in D.

Great! That's exactly what I was proposing.



More information about the Digitalmars-d mailing list