tags:

views:

384

answers:

5

Hi, I have to use unsigned integers that could span to more than 4 bytes, what type should I use?

PS Sorry for the "noobism" but that's it :D

NB: I need integers because i have to do divisions and care only for the integer parts and this way int are useful

+5  A: 

long long, 64 bit integer... here you can find some reference about the data types and ranges...

CMS
Thank you, didn't know I can use "double" long :D
luiss
Use <stdint> as described in CesarB's answer for a *portable* solution. GCC doesn't understand __int64, MSVC6 (and possibly 2003) don't understand "long long", and neither is a standard.
ephemient
if you require more than 4 bytes, this is not the cleanest way.
Dustin Getz
Actually, "long long" *is* in a standard, C99, and will be in C++0x according to Wikipedia.
CesarB
+1  A: 

unsigned long long - it is at least 64 bits long

Dmitry Khalatov
+9  A: 

Simply include <stdint.h> and use int64_t and uint64_t (since you want unsigned, you want uint64_t).

There are several other useful variants on that header, like the least variants (uint_least64_t is a type with at least 64 bits) and the fast variants (uint_fast64_t is the fastest integer type with at least 64 bits). Also very useful are intptr_t/uintptr_t (large enough for a void * pointer) and intmax_t/uintmax_t (largest type).

And if for some reason your compiler doesn't have a <stdint.h> (since IIRC it's a C standard, not a C++ one), you can use Boost's boost/cstdint.hpp (which you can use even if you do have a <stdint.h>, since in that case it should simply forward to the compiler's header).

CesarB
<stdint.h> is in C99, not older C standards, for what it's worth. Furthermore, an implementation must provides uint64_t and int64_t if and only if it provides the corresponding 64 bit integer type. A C99 implementation is not required to provide one, but it is extremely likely to.
Chris Young
+2  A: 

Take your pick:

long long (–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)

unsigned long long: (0 to 18,446,744,073,709,551,615)

Andrew
+1  A: 

If you need really long integers (arbitrary precision), you could also try the gmp library, which also provides a C++ class based interface.

Kim Sullivan