Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7921321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:32:23+00:00 2026-06-03T16:32:23+00:00

I wrote a small function to convert MB to Bytes, however, there seems to

  • 0

I wrote a small function to convert MB to Bytes, however, there seems to be a bug in int64. According to the documentation, int64 ranges from -9223372036854775808 to 9223372036854775807, but my results differ…a lot:

Const FreeSpace = 67100;

var FreeSpaceConverted :int64;
.
.
.

FreeSpaceConverted := FreeSpace shl 20;

Using a value of 67100 for FreeSpace results in a value of 1639972864 instead of 70359449600. It’s obvious that the conversion ran out of space and wrapped around. The actual size of int64 seems to be 70359449600 – 1639972864 = 68719476736 = 2^36 while it should be 2^63-1. An exponent of 36 looks rather strange. Could it be a number twist in the compiler itself??

Also, using the following alternative gives the error “Overflow in conversion or arithmetic operation” even though it shouldn’t:

FreeSpaceConverted := FreeSpace * 1024 * 1024;

On the other hand, the following alternative does work:

FreeSpaceConverted := FreeSpace * 1024;
FreeSpaceConverted := FreeSpaceConverted * 1024;

Is this normal behavior and if so, what’s the reason for all of this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:32:26+00:00Added an answer on June 3, 2026 at 4:32 pm

    All the code you include in the question works fine in Delphi 7.

    program Int64Test;
    
    {$APPTYPE CONSOLE}
    
    var
      FreeSpaceConverted, FreeSpace: Int64;
    
    begin
      FreeSpace := 67100;
      FreeSpaceConverted := FreeSpace shl 20;
      Writeln(FreeSpaceConverted);
      FreeSpaceConverted := FreeSpace * 1024 * 1024;
      Writeln(FreeSpaceConverted);
      Readln;
    end.
    

    Output

    70359449600
    70359449600
    

    Your actual code is different from what you have stated in the question. In fact, FreeSpace is declared as a 32 bit type in your code, probably Integer. For example, and I’m having to guess a little here:

    program Int64Test;
    
    {$APPTYPE CONSOLE}
    
    var
      FreeSpace: Integer;
      FreeSpaceConverted: Int64;
    
    begin
      FreeSpace := 67100;
      FreeSpaceConverted := FreeSpace shl 20;
      Writeln(FreeSpaceConverted);
      FreeSpaceConverted := FreeSpace * 1024 * 1024;
      Writeln(FreeSpaceConverted);
      Readln;
    end.
    

    Output

    1639972864
    1639972864
    

    If we enable overflow checking then the multiplication code results in an overflow exception, as you report.


    Now consider FreeSpace shl 20 when FreeSpace is an integer. The compiler interprets this as a 32 bit integer operation and shifts the more significant bits off the end of the 32 bit register. The fact that you are assigning to a 64 bit integer is not relevant. What matters are the data types in the expression. You can make the code behave the way you want by including a cast to Int64 on the right hand side.

    program Int64Test;
    
    {$APPTYPE CONSOLE}
    
    var
      FreeSpace: Integer;
      FreeSpaceConverted: Int64;
    
    begin
      FreeSpace := 67100;
      FreeSpaceConverted := Int64(FreeSpace) shl 20;
      Writeln(FreeSpaceConverted);
      FreeSpaceConverted := Int64(FreeSpace) * 1024 * 1024;
      Writeln(FreeSpaceConverted);
      Readln;
    end.
    

    Output

    70359449600
    70359449600
    

    For a fuller discussion I refer you to Barry Kelly’s answer to another question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a small function in C# that manipulates pdf files using itextsharp. Is
I wrote a small program using a custom indexOf function but wanted to dismiss
I wrote a small tcp client using boost::asio, providing the following function: typedef boost::function<void(const
The http.get() function inside http.createServer is not responding. I wrote a small snippet to
A PHP function I am writing pulls a small bit of HTML data from
I wrote a small scroller in jquery. The scroller seems to work perfectly on
As a side result of testing some code I wrote a small function to
So, I wrote a small page: <html> <head> <script type=text/javascript src=jquery.js></script> <script> function slide()
i wrote a small function to do this job x = 'INV-%05d' m =
I'm using AMFPHP, and wrote a small function in php to post in facebook

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.