Calculation of file size, Int64 and differences between 32-bit and 64-bit

I had problems with the following code:

var
  FileSize : Int64;
...
FileSize := Info.nFileSizeLow or (Info.nFileSizeHigh shl 32);

I expected it to work due to the type of Int64left side of the job. But this is not so. A partial calculation that shlappears to cause an overflow.

So I changed it to:

FileSize := Info.nFileSizeLow or (Int64 (Info.nFileSizeHigh) shl 32);

which works on my 32 bit operating system but does not work on Vista 64 bit!

Finally,

FileSize := Info.nFileSizeHigh;
FileSize := FileSize shl 32;
FileSize := Info.nFileSizeLow or FileSize;

works on both systems.

Can someone explain the differences in these three versions?

+3
source share
5 answers

a * b, a b Integer * - , Integer, , Integer. ( , , /.) , 64- , , 64- . , 64- 64- .

, 64- , . , 32- 64- ( ..); , .

, . , , , :

var a, b: Integer;
// ...
P((a shl 16) or b); // 32-bit operation or 64-bit operation?

, . , , . , 32- Windows, 64- Windows, .

+5

, Delphi 7 " ":

Integer, 32- Longint. Int64 Int64. , .

:

var
  I: Integer;
  J: Int64;
  ...
I := High(Integer);
J := I + 1;

Int64 , Int64:

 ...
J := Int64(I) + 1;
+2

FileSize UInt64, Int64...

UInt64 ( Delphi) 64- , QWORD. FileSize ( , ?).

IMHO - UInt64, , :

FileSize := UInt64(Info.nFileSizeLow) or (UInt64(Info.nFileSizeHigh) shl 32));

Delphi 7 , .

FileSize := Info.nFileSizeLow or (Int64(Info.nFileSizeHigh) shl 32));

, , . asm- ( , Alt + F2) , . ...

( ) :

with Int64Rec(FileSize) do
begin
  Lo := Info.nFileSizeLow;
  Hi := Info.nFileSizeHigh;
end;

MSDN WIN32_FIND_DATA:

nFileSizeHigh: DWORD .

, MAXDWORD.

(nFileSizeHigh * (MAXDWORD + 1)) + nFileSizeLow.

nFileSizeLow: DWORD .

:

FileSize := UInt64(Info.nFileSizeLow)+(UInt64(Info.nFileSizeHigh)*UInt64(1 shl 32));

, ...

+1

, .

, Delphi , 64- , 32 . , -, 64- . , 64- , Delphi , 64- .

, (1) (2) , Delphi 32- 64- . , 32- - " " (.. , ). , COMPILED 32- 64- . , , CPU, , CPU, " ".

0

test for Delphi 7 and version 2 is fine. There must be a later error

0
source

Source: https://habr.com/ru/post/1791247/


All Articles