D Idioms
Patrick Schluter
Patrick.Schluter at bbox.fr
Fri Oct 2 15:53:24 UTC 2020
On Friday, 2 October 2020 at 15:22:37 UTC, H. S. Teoh wrote:
> On Fri, Oct 02, 2020 at 03:12:01PM +0000, Guillaume Piolat via
> Digitalmars-d wrote: [...]
>> For example, did you know that shifting an integer by more
>> than 31 is Undefined Behaviour?
>
> This is not surprising; even in C/C++ it's well-known to be UB.
>
Yes, and the reason is due to the shift instruction in the CPU's.
Some CPU's do the right thing like PowerPC and some do the wrong
thing like x86. On x86 SHL instruction masks the shift number
that is used, so
uint x=1;
x << 32
is equivalent to
x << (32 & 0x1F) // 0x3F in 64 bits mode
which is the same as
x
and not 0 as
If the behaviour was fixed, the compiler would have to add a test
in some implementations.
More information about the Digitalmars-d
mailing list