8

I want to use 64 bit integers in my C++ code. I understand I can either #include <cstdint> and then declare a uint64_t or use unsigned long long (or the equivalent for signed versions).

However, it appears that support for this was not added until C++11 and I would like my code to be compatible with compilers that don't have full C++11 support.

What is a good portable way to support 64 bit integers in C++?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Simd
  • 19,447
  • 42
  • 136
  • 271
  • There's probably a Boost library for this – Justin Jun 15 '17 at 18:43
  • [uint64_t](http://en.cppreference.com/w/cpp/types/integer) is optional, even with C++11, so that is not portable either. – IInspectable Jun 15 '17 at 18:44
  • Are you looking for things like `uint64_t`, `uint32_t`, etc or specifically `uint64_t`? – Justin Jun 15 '17 at 18:49
  • @Justin I am specifically interested in 64 bit integers. – Simd Jun 15 '17 at 18:49
  • Do you need something guaranteed 64 bit or are you okay with at least 64 bit support? – NathanOliver Jun 15 '17 at 18:52
  • @NathanOliver In practice I will iterate over the integers calling __builtin_ctz on each one. If it is more than 64 bits will it give the same answer? – Simd Jun 15 '17 at 18:55
  • I have no idea. My idea didn't pan out though so I guess it doesn't matter, sorry. – NathanOliver Jun 15 '17 at 18:58
  • @doynax Thanks. Should `__builtin_clzll` give the same answer whether you use `long long` (at least 64 bits) or `int64_t` (exactly 64 bits)? – Simd Jun 15 '17 at 19:04
  • 1
    @eleanora: No, I suppose you'll need something along the lines of `__builtin_clzll(value) + 64 - CHAR_BIT * sizeof(unsigned long long)` if you should manage to encounter a platform with `long long` wider than 64-bits. – doynax Jun 15 '17 at 19:11

1 Answers1

7

uint64_t is:

Optional: These typedefs are not defined if no types with such characteristics exist.

as you can read in the ref.


From Should I use long long or int64_t for portable code?:

The types long long and unsigned long long are standard C and standard C++ types each with at least 64 bits. All compilers I'm aware of provide these types, except possibly when in a -pedantic mode but in this case int64_t or uint64_t won't be available with pre-C++ 2011 compilers, either. "


What date did g++/clang support long long/int64_t from?

Since GCC 4.3 (aka March 5, 2008).

As David Álvarez mentioned.

gsamaras
  • 71,951
  • 46
  • 188
  • 305