🔒 Locked
long long int data type
Advertisement
Original Post
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?
long long int is C99. If you want to check for C99 compatibility (sidenote: C++-2003 is not C99 compliant):
See also this site for a comparison of C++-1998 and C99. Summary: You should be able to use
or
which provide uint64_t and int64_t.
(see also this for a concise list of predefined compiler macros)
#if __STDC_VERSION__ >= 199901L // newer than c99C99 code goes here#endifSee 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)
I'd use <boost/cstdint.hpp>
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?
And if that's the case, what's the size of an int on a 64-bit machine? And a long?
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).
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).
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.
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.
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.
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.
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 :)
Quote:
Original post by 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. :)
Plus a char is always 1 byte :D
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!
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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement