# The result of Delphi strange, what reason is caused?

The following code
var
dx,m,ax,a:Integer;
begin
a :=29959314 ;
m :=-3;
ax :=(a*m) shr 21;
dx :=(29959314*-3) shr 21;
end;
Know the results of the ax? 2005, know the results of the DX? -43, what the hell, Delphi7 how two results, the correct result is -43, because the C++ execution result is -43, who know how to solve? Since 29959314 and -3 two values will change, I found that I was wrong. So when debugging

Started by Kristine at February 04, 2016 - 2:16 PM

Here I say, ax: = (a*m) SHR 21; changed to AX: =int64 (a*m) SHR 21 results is correct, but who can explain why not add Int64 error?

Posted by Kristine at February 07, 2016 - 2:58 PM

Because c++'s right is a symbol to the right, the corresponding assembler instruction is SAR
While Delphi's right is the unsigned right shift, corresponding assembler instruction is SHR
Look at the both disassembly will know.

Posted by Marsh at February 22, 2016 - 3:25 PM

This causal relationship

Posted by Tracy at March 04, 2016 - 3:52 PM

The unified method simply inline assembler.

```var
a,m,aa:Integer;
begin
a :=29959314 ;
m :=-3;
asm
push eax
push ebx
mov eax, a
mov ebx, m
imul eax, ebx
sar eax,21
mov aa, eax
pop ebx
pop eax
end;```

Posted by Marsh at March 18, 2016 - 4:13 PM

I use XE2 to test, the result is the same.

Posted by Troy at April 02, 2016 - 4:23 PM

All 2005

Posted by Troy at April 07, 2016 - 5:11 PM

2005 is to, Delphi SHR only logical right shift, and signed or unsigned operands, shall be unsigned number processing, which in the Turbo Pascal era is a formal characteristics in document, and C and C++ > > the different processing method.
The result in -43, a compiler is evaluated at compile time (29959314*-3) SHR 21; do not correct (and possibly Delphi and CB share the compiler backend caused by BUG, but has since corrected).

Posted by Devin at April 12, 2016 - 5:59 PM

In fact, the reason is very simple, because the default integer only two bytes, the effective range to plus or minus 60000, you give him such a large number, nature will overflow, overflow can guarantee figures look like to say.
Int64 default is 8 bytes, the effective range of more than 10 digits, not overflow, natural right.

Posted by Zoe at April 14, 2016 - 6:15 PM

n b ,By default Delphi will be an integer constant to the int32bit near, if the constants assigned to variables, strengthen system into Int64.
In the Delphi XE5 is still such, Lazarus is right.

<<Delphi trap>>

Posted by Ken at January 14, 2017 - 2:03 PM