Why do shift operators undergo integral promotion?
KennyTM~
kennytm at gmail.com
Wed Aug 10 00:11:15 PDT 2011
On Aug 10, 11 01:06, Walter Bright wrote:
> On 8/9/2011 2:46 AM, Don wrote:
>> From a discussion on D.learn.
>>
>> If x and y are different integral types, then in an expression like
>> x >> y
>> the integral promotion rules are applied to x and y.
>> This behaviour is obviously inherited from C, but why did C use such a
>> counter-intuitive and bug-prone rule?
>> Why isn't typeof(x >> y) simply typeof(x) ?
>> What would break if it did?
>>
>> You might think the the rule is that typeof( x >> y) is typeof( x + y),
>> but it isn't: the arithmetic conversions are NOT applied:
>> typeof(int >> long) is int, not long, BUT
>> typeof(short >> int) is int.
>> And we have this death trap (bug 2809):
>>
>> void main()
>> {
>> short s = -1;
>> ushort u = s;
>> assert( u == s );
>> assert ( (s >>> 1) == (u >>> 1) ); // FAILS
>> }
>
>
> That last is why we can't just change the behavior from C.
Does C or C++ even have a '>>>' operator? If we need to have a
type-promotion rule like C, it could be made as
x >>> y == cast(promoted type) cast(typeof(x)) (unsigned(x) >> y)
e.g.
cast(short)(-1) >>> 1 == 0x7f.
More information about the Digitalmars-d
mailing list