CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2001
    Location
    Victoria, BC, Canada
    Posts
    363

    __int64 and problems

    I need to calculate the sizes in bytes of some very large files so I started playing around with unsigned __int64 datatypes.

    As a test, I took 0x40000000 and said basically: num = _GB * 2;

    (_GB being the hex number above)

    and the result in num was: 0xffffffff80000000

    Is there a compiler option I need to make this work properly, or am I not understanding something about these 64 bit types? I thought it should just be 0x80000000 without the leading 0xffffffff? Or am I totally out to lunch here?

    Thanks!

  2. #2
    Join Date
    May 2002
    Posts
    1,435

    Re: __int64 and problems

    Since 0x40000000 and 2 are both valid integers the multiplication will be done as integers before assigning to int64 so you will have overflow.

    The solution is to explicitly cast at least one of the integers to an int64 before the multiplcation:

    __int64 num = (__int64)0x40000000 * 2;

  3. #3
    Join Date
    Sep 2001
    Location
    Victoria, BC, Canada
    Posts
    363

    Re: __int64 and problems

    Gotcha, thank you!

  4. #4
    Join Date
    Nov 2006
    Posts
    120

    Re: __int64 and problems

    Quote Originally Posted by 0xC0000005
    The solution is to explicitly cast at least one of the integers to an int64 before the multiplcation:

    __int64 num = (__int64)0x40000000 * 2;
    No need to cast. Simply use the LL suffix on one of the integers:

    Code:
        __int64 a;
        a = 12345678LL * 12345678;
    (LL means "long long").

    In any case it is recommended (by Bjarne himself ) that old C-style casts are not used since their intended meaning is less well understood by the compiler: http://www.research.att.com/~bs/bs_f...ml#static-cast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured