[fpc-devel] bitwise shift oddity a << b

Martin Frb lazarus at mfriebe.de
Thu May 14 05:41:20 CEST 2015


What is supposed to happen if the 2nd argument is negative?

program Project1;
uses sysutils;
var  a: LongInt;
begin
a := StrToInt('-1');
WriteLn(2 << a);
WriteLn(2 << -1);
ReadLn;
end.

prints:
0
4294967296

"2 << a" translates to "shl eax,cl" (cl being loaded with the result 
from strtoint)

So at least my cpu thinks the result is 0. (I guess unsigned byte(-1) 
makes it "2 << 255")
But the compiler (3.1.1) pre-evaluating the constant, comes to a diff 
result.






More information about the fpc-devel mailing list