D arithmetic problem
Steven Schveighoffer
schveiguy at yahoo.com
Thu Jun 4 06:58:24 PDT 2009
On Thu, 04 Jun 2009 00:06:33 -0400, Don <nospam at nospam.com> wrote:
> Steven Schveighoffer wrote:
>> 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).
I'm afraid you are thinking way deeper than I do when looking at it :)
I say "I want an 8-bit integer, what does D have? (browses to
http://www.digitalmars.com/d/1.0/type.html) Oh, it's byte! cool. what's
the unsigned version? Don't even have to look that up, follow the
convention: ubyte!"
Granted, if it had said sbyte instead of byte, I'd probably have some
misgivings about how it doesn't follow the convention of the other types,
but I'd have gotten over that pretty quick.
The O.P. though, probably didn't go through the same thought process you
did. He was converting C# code, so he logically thought Byte -> byte.
Makes sense. If C# had made Byte signed, then we wouldn't even be having
this discussion...
>> 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.
At least we agree there :)
-Steve
More information about the Digitalmars-d
mailing list