Problems with shift left operator (dmd 0.169)
Lionello Lunesu
lio at lunesu.remove.com
Tue Oct 10 23:56:48 PDT 2006
KlausO wrote:
> Hello D-experts,
>
> I created a little test program which IMHO shows
> not the expected behaviour.
> Is this a bug or not ?
>
> module testshift;
>
> import std.stdio;
>
> void main()
> {
> uint val_a = (1 << 32);
>
> uint shift = 32;
> uint val_b = (1 << shift);
>
> writefln("Result: ", val_a, " ", val_b);
> assert(val_a == val_b);
> }
>
> Output is as follows:
> Result: 0 1
> Error: AssertError Failure testshift(14)
The problem here is: what values would you expect?
1<<32 is too big for an uint, so the 0 from the constant folding is
correct, BUT the "shl" instructions (the one << translates into) only
looks at the lower 5 bits, which are 0 (32 & 0x1F == 0), so also the 1
is correct.
I don't think we'd want extra overhead for something like <<, so I
suppose we let "<<" behave like the instruction "shl". Which means
there's only one solution: let the compiler complain.
Come to think of it, it should already have complained: 1<<32 does not
fit into an uint! But DMD gives no error, not even a warning (-w)...
L.
More information about the Digitalmars-d-learn
mailing list