|
-
August 24th, 2007, 12:30 PM
#1
__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!
-
August 24th, 2007, 01:16 PM
#2
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;
-
August 24th, 2007, 01:23 PM
#3
-
August 24th, 2007, 06:38 PM
#4
Re: __int64 and problems
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|