Bit operator conversions

Don nospam at nospam.com
Thu Apr 9 03:19:38 PDT 2009


Jarrett Billingsley wrote:
> 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.

cast.c line 50.

		/* This is really only a semi-kludge fix,
		 * we really should look at the operands of op
		 * and see if they are narrower types.
		 * For example, b=b|b and b=b|7 and s=b+b should be allowed,
		 * but b=b|i should be an error.
		 */

Sure, put it in bugzilla.


More information about the Digitalmars-d-learn mailing list