D arithmetic problem

Denis Koroskin 2korden at gmail.com
Thu Jun 4 00:33:37 PDT 2009


On Thu, 04 Jun 2009 10:00:27 +0400, Don <nospam at nospam.com> wrote:

> bearophile wrote:
>> Denis Koroskin:
>>> Shouldn't bitwise operations be disallowed on signed types at all?
>>  It sounds OK to me, do Don & Walter agree?
>
> Hmm. If you are doing bitwise operations, you are treating the number as  
> unsigned, no doubt about it. Some observations:
>
> * The use of -1 for "all bits set" is in widespread use and is important.
>
> ushort x ^= -1;
> uint y ^= -1;
> ulong z ^= -1;
> probably needs to remain valid.
> But then consider
>   (x ^ -1) + y
>
> What is the type of x^-1 ? Is it ushort? Or int?
>

I see no problem adding implicit cast to unsigned counterparts here.
In this case size of (x ^ cast(uT)-1) would be unambiguous.

> * A lot of existing C code uses bitwise operations on ints.

That's fine, raising an error in such cases is a good behavior.

> * size_t had better be unsigned!



More information about the Digitalmars-d mailing list