Bug in shifting

Rainer Schuetze r.sagitario at gmx.de
Tue Dec 18 20:33:43 UTC 2018



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.


More information about the Digitalmars-d-learn mailing list