[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 07:12:26 UTC 2023


https://issues.dlang.org/show_bug.cgi?id=23618

Salih Dincer <salihdb at hotmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |salihdb at hotmail.com

--- Comment #3 from Salih Dincer <salihdb at hotmail.com> ---
The problem is also seen when the sign extension is not done.

Here is the test code:

struct TestType(T)
{
  import std.traits : Unsigned;
  alias U = Unsigned!T;

  T t = T.min;       // sample: -128 for byte
  U u = U.max/2 + 1; // sample: 128 for ubyte
}

void main()
{
  alias T = long; // int, short, byte

  TestType!T v1, v2;
  enum bits = T.sizeof * 8 - 1;

  v1.t >>= bits;
  assert(v1.t == -1); // okay, because signed type

  v1.u >>= bits;
  assert(v1.u == 1); // okay, because unsigned type

  v2.t >>>= bits;
  assert(v2.t == 1); /* okay, no sign extension
                        but  -1 for byte, short, int */
  v2.u >>>= bits;
  assert(v2.u == 1); // okay, no sign extension
}

SDB at 79

--


More information about the Digitalmars-d-bugs mailing list