Bug in shifting
Patrick Schluter
Patrick.Schluter at bbox.fr
Wed Dec 19 17:05:55 UTC 2018
On Tuesday, 18 December 2018 at 20:33:43 UTC, Rainer Schuetze
wrote:
>
>
> On 14/12/2018 02:56, Steven Schveighoffer wrote:
>> On 12/13/18 7:16 PM, Michelle Long wrote:
>>> byte x = 0xF;
>>> ulong y = x >> 60;
>>
>> Surely you meant x << 60? As x >> 60 is going to be 0, even
>> with a ulong.
>
> It doesn't work as intuitive as you'd expect:
>
> void main()
> {
> int x = 256;
> int y = 36;
> int z = x >> y;
> writeln(z);
> }
>
> prints "16" without optimizations and "0" with optimizations.
> This happens for x86 architecture because the processor just
> uses the lower bits of the shift count. It is probably the
> reason why the language disallows shifting by more bits than
> the size of the operand.
Yes. On x86 shifting (x >> y) is in reality x >> (y & 0x1F) on 32
bits and x >> (y & 0x3F) on 64 bits.
More information about the Digitalmars-d-learn
mailing list