Bit operator conversions
Jarrett Billingsley
jarrett.billingsley at gmail.com
Wed Apr 8 11:28:49 PDT 2009
On Mon, Apr 6, 2009 at 8:53 AM, Kagamin <spam at here.lot> wrote:
> Is it valid for this to compile:
> ---
> ushort a(ushort b) pure nothrow
> { return b<<10|b; }
> ---
>
> And for this to not compile:
> ---
> ushort a(ushort b) pure nothrow
> { return b<<10; }
> ---
> ?
There was a terribly long conversation about this and other operations here:
http://d.puremagic.com/issues/show_bug.cgi?id=1977
Basically the idea behind the warning on the left-shift is that you
can't know, at compile-time, whether the shift will overflow the size
of ushort or not. If you passed in 0xFFFF, for instance, it would
overflow. So it converts left-shifts to int and complains if you
don't have an explicit cast.
But for many bitwise operators, such as | and &, there is no risk of
an overflow at runtime, so if your function returned "b & 0x3F00", you
wouldn't get such a warning.
That it accepts "b << 10 | b" but rejects "b << 10", however, looks
more like a bug. It's like the compiler isn't doing enough work to
find out whether the former can overflow or not.
More information about the Digitalmars-d-learn
mailing list