[Issue 23618] Right Shift equals expressions on unsigned shorts don't behave the same as regular shifts
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Jan 14 06:50:59 UTC 2023
https://issues.dlang.org/show_bug.cgi?id=23618
Walter Bright <bugzilla at digitalmars.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |bugzilla at digitalmars.com
--- Comment #2 from Walter Bright <bugzilla at digitalmars.com> ---
The issue comes up because (ee >>= 5) is done as a signed right shift, even
though ee is an unsigned type.
Step by step:
https://dlang.org/spec/expression.html#assignment_operator_expressions says:
a op= b
are semantically equivalent to:
a = cast(typeof(a))(a op b)
so, (ee >>= 5) is rewritten to:
ee = cast(ushort)(ee >> b);
https://dlang.org/spec/expression.html#shift_expressions says:
the operands undergo integral promotions
ee = cast(ushort)(cast(int)ee >> b)
or:
ee = cast(ushort)((ee & 0x0000_FFFF) >> b)
then:
>> is a signed right shift
but the sign bit is 0, so it is, in effect, an unsigned right shift. The
compiler generates a signed right shift, though.
--
More information about the Digitalmars-d-bugs
mailing list