Skip to main content
gamedev.net
🔒 Locked

long long int data type

Started by CDProp 16 replies 2.2k views
Original Post
CDProp
CDProp
I just found out about this today. I had been using "rollover counts" in order to keep track of numbers that climb higher than what 32 bits allow. I had no idea there was a long long type that is, I presume, 64 bits. More, maybe? Is this part of the standard? Or YMMV depending on what compiler or CPU you're on?
Cantos
Cantos
If you want a 64 bit integral data type, there are some portable ones in boost. IIRC long long is going to be part of C++0x, but is implemented by some compilers already.
phresnel
phresnel
long long int is C99. If you want to check for C99 compatibility (sidenote: C++-2003 is not C99 compliant):

#if __STDC_VERSION__ >= 199901L // newer than c99C99 code goes here#endif


See also this site for a comparison of C++-1998 and C99. Summary: You should be able to use
#include <cstdint>

or
#include <stdint.h>

which provide uint64_t and int64_t.

(see also this for a concise list of predefined compiler macros)
gekko
gekko
C99 and C++0x. Many compilers have included it, and many compilers have their own size-specific numbers you can use. You can always typedef your own and change it depending on the compiler/language versions.
-- gekko
XTAL256
XTAL256
I thought 64-bit ints (long long) had always been in C/C++, at least since 32-bit machines came out. Actually, i always thought that "long int" was 64-bit since "int" was 32-bit. It wasn't until recently that i found out that was only in Java and that in C/C++ longs were the same as ints because, historically, ints were only 16-bit (one word on a 16-bit machine) so long ints were therefore 32-bits. Seems pretty stupid that they changed the size of ints but not longs (unless i am wrong), but that's the problem with progress and backwards compatibility.
And if that's the case, what's the size of an int on a 64-bit machine? And a long?
[Window Detective] - Windows UI spy utility for programmers
SiCrane
SiCrane
Quote:
Original post by XTAL256
And if that's the case, what's the size of an int on a 64-bit machine? And a long?

It depends on the compiler and the machine.
samoth
samoth
long and int are not defined precisely, neither under 32 nor 64 bits.
Usually they are both 32 bits on a 32 bit machine and 64/32 bits on a 64 bit machine, but that is not necessarily the case. The only guarantee that you have by the standard is that int will be "a size that the CPU can handle efficiently", and long will be least as large as int (possibly larger, never smaller).

The forementioned <cstdint> header (or the boost one, if you want) is useful if you want to be sure a variable has a particular size, such as 64 bits. There are also types that guarantee a minimum size in it (though I've never had a need to use these).
SiCrane
SiCrane
No, you've got two additional guarantees: int is guaranteed to be at least 16 bits and long is guaranteed to be at least 32 bits.
Vortez
Vortez
Really? i though LONG was supposed to be fixed to 32-bits, no?
... Or maybe im mistaken with DWORD.
XTAL256
XTAL256
Well i think, technically, a DWORD depends on the system too. A word is 16 bits on a 16-bit machine, 32 bits on a 32-bit machine, etc.
I would think that logically a long is longer than an int (duh!) so it should always be twice the size of an int. Why they changed int to be 32 bits for a 32-bit system but kept long the same i will never know.
[Window Detective] - Windows UI spy utility for programmers
MaulingMonkey
MaulingMonkey
Quote:
Original post by Vortez
Really? i though LONG was supposed to be fixed to 32-bits, no?
... Or maybe im mistaken with DWORD.


LONG and long are two distinct types.
LONG is a Win32 API type, and is meant to be fixed at 32 bits for backwards compatibility.
long is a C and C++ standard type, which means it is very flexible in terms of exactly how large it is. Relevant details.

"But my longs are always 32 bits!" you might say. "So why do I care? Stop making shit up!"

Consider that BOOL is typically a typedef to int, even in C++, not bool. C89 didn't have an integral bool type, you see.
daviangel
daviangel
Quote:
Original post by SiCrane
Quote:
Original post by XTAL256
And if that's the case, what's the size of an int on a 64-bit machine? And a long?

It depends on the compiler and the machine.

Yup for the MS compiler for example adding the long type specifier to a long int making it a long long it's actually bigger than a long at 64bits. Doing the same thing to a double i.e. long double has no effect.

The representation of long double and double is identical. However, long double and double are separate types.
So your thinking what's the point if they are exactly the same size?
Well in the older MS C++ compiler the long double actually was larger than a double at 80bits but that's another posts I've posted about in the past.

[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Zahlman
Zahlman
Quote:
Original post by SiCrane
No, you've got two additional guarantees: int is guaranteed to be at least 16 bits and long is guaranteed to be at least 32 bits.


And char and short are at least 8. :)

(Honestly, I'm disappointed in Boost's answer to this stuff being just preprocessor hackery. I would have hoped for a generic integer class templated on a bit count, using boost::enable_if etc. to substitute native types in the appropriate template specializations and some generic wrapper class for other sizes. Actually, I still have plans to write that :)
SiCrane
SiCrane
Quote:
Original post by Zahlman
And char and short are at least 8. :)

Hmm. I see SHRT_MAX as having a minimum value of 32767, which would mean that short is at least 16.
nobodynews
nobodynews
Quote:
Original post by SiCrane
Quote:
Original post by Zahlman
And char and short are at least 8. :)

Hmm. I see SHRT_MAX as having a minimum value of 32767, which would mean that short is at least 16.


16 is at least 8!
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
magic_man
magic_man
Quote:
Original post by nobodynews
Quote:
Original post by SiCrane
Quote:
Original post by Zahlman
And char and short are at least 8. :)

Hmm. I see SHRT_MAX as having a minimum value of 32767, which would mean that short is at least 16.


16 is at least 8!


There is always one "16 is at least 8" yes but this is not the same as "short is at least 16".

The C standard says that:
CHAR_BIT is at least 8
short has a least 16 bits
int has at least 16 bits
long has at least 32 bits
and long long has at least 64 bits.
"You insulted me!" I did not say that in the private message Tom Sloper!
Zahlman
Zahlman
Quote:
Original post by SiCrane
Quote:
Original post by Zahlman
And char and short are at least 8. :)

Hmm. I see SHRT_MAX as having a minimum value of 32767, which would mean that short is at least 16.


I was being conservative due to not having checked. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.