Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

mdog1234

__int64

This topic is 5184 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i want to compute numbers greater than 4294967296 so i am using __int64. the only problem is that I dont know how to print them to a file or to the screen or whatever. just using printf wont work im sure. can anyone help or does anyone know a better way to compute very large numbers?

Share this post


Link to post
Share on other sites
Advertisement
As long as we''re on the subject, I''m trying to write two macros:

#define HIDWORD(x) ((int)((x)>>32))
#define LODWORD(x) ((int)(x))

Or, also reasonably effective:

__forceinline int HIDWORD(__int64 x) { return ((int*)&x)[1]; }
__forceinline int LODWORD(__int64 x) { return ((int*)&x)[0]; }

The first two macros generate good code when x is a constant, but generate poor code when x is a variable.

The latter two inlines generate good code when x is a variable, but generate poor code when x is a constant.

Anybody think of another way to do this?

Share this post


Link to post
Share on other sites
quote:
Original post by Aprosenf
I think you can print an __int64 like this:

printf("%I64", myInt64);
Actually, the format string needs to be "%I64d". The "I64" part is just like using a 'h' to indicate that you're printing a short, not a long.

[edited by - Agony on April 6, 2004 4:12:35 PM]

Share this post


Link to post
Share on other sites
In MSVC, do this:

unsigned __int64 uint64;
printf("%I64u\n", uint64); // print unsigned

printf("%I64x\n", uint64); // print unsigned in hex

__int64 int64;
printf("%I64d\n", int64); // print signed

If you are using GCC, etc.:

unsigned long long uint64;
printf("%llu\n", uint64); // print unsigned

printf("%llx\n", uint64); // print unsigned in hex

long long int64;
printf("%lld\n", int64); // print signed

It is usually a good idea to use a typedef to define your 64-bit int. For instance:

typedef unsigned __int64 uint64;
typedef __int64 int64;

That way if you ever need to compile your program with something other than MSVC, you can do so by changing a few lines.

Also worth noting is that if you use GCC, you will have to append "ull" or "ll" to the end of 64-bit constants. For instance, if you did this in MSVC:

uint64 x = 0x1234567890123456;

You would have to do this in GCC:

uint64 x = 0x1234567890123456ull;

Lastly, you can do this with C++ and cout as well, but not all compilers can handle it. GCC can. MSVC can''t (last time I checked). In GCC, you can just do:

unsigned long long x = 0x1234567890abcdefull;
cout << x << endl;

Share this post


Link to post
Share on other sites
Yeah, I use MSVC6, but I wanted to use cout with __in64, so I just overloaded <<
std::ostream& operator << (std::ostream& Out, const __int64& n)
{
char Buffer[32];
_snprintf(Buffer, 31, "%I64d", n);
Out << Buffer;
return Out;
}
There may be slightly better ways to do it, but I just needed something quick. I just threw it in my utilities file. Doing the same for cin (>>) was gonna be a bit nastier, I think, so I just didn't bother.

[edited by - Agony on April 7, 2004 11:08:41 AM]

Share this post


Link to post
Share on other sites
You might want to make a header file that has something like:
#ifdef _MSC_VER

typedef __in64 int64;
typedef unsigned __int64 uint64;

#define _64(x) x ## i64
#define _u64(x) x ## ui64

#else

typedef long long int64;
typedef unsigned long long uint64;

#define _64(x) x ## ll
#define _u64(x) x ## ull

#endif


So that in your main code, you could have

int64 foo = _64(0x0123456789abcdef);
uint64 bar = _u64(0xfedcba9876543210);

Share this post


Link to post
Share on other sites
quote:
Original post by Aprosenf
#define _64(x) x ## ll
#define _u64(x) x ## ull
int64 foo = _64(0x0123456789abcdef);
uint64 bar = _u64(0xfedcba9876543210);



I believe that underscores followed by a lower case letter are reserved for the compiler implementers. In other words, you shouldn''t name things starting with an underscore and a lowercase letter. I generally avoid starting anything with an underscore.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!