January 12, 2023

On Thursday, 12 January 2023 at 04:01:13 UTC, Ali Çehreli wrote:

>

On 1/11/23 15:54, Salih Dincer wrote:

>

The problem is that it shift via int first.

Yes but it shouldn't because one of the operands is uint and the 'int' operand should be converted to uint and the operation should have uint semantics.

Deadalnix is RIGHT!

Interestingly, triple shifting (>>>) does not work correctly on any type, except long!

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@79

January 14, 2023
https://github.com/dlang/dmd/pull/14814
1 2 3
Next ›   Last »