File size calculation, Int64, and differences between 32bit and 64bit

后端 未结 5 1635
走了就别回头了
走了就别回头了 2021-01-19 06:27

I had problems with the following code:

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

I expecte

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-19 06:57

    Actually, this is pretty well documented in Delphi 7's help file, under "Integer types":

    In general, arithmetic operations on integers return a value of type Integer--which, in its current implementation, is equivalent to the 32-bit Longint. Operations return a value of type Int64 only when performed on one or more Int64 operand. Hence the following code produces incorrect results.

    The code example provided:

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

    To get an Int64 return value in this situation, cast I as Int64:

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

提交回复
热议问题