64 bit integers and older C++ compilers
Asked Answered
S

1

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++?

Santiagosantillan answered 15/6, 2017 at 18:40 Comment(9)
There's probably a Boost library for thisWendall
uint64_t is optional, even with C++11, so that is not portable either.Storage
Are you looking for things like uint64_t, uint32_t, etc or specifically uint64_t?Wendall
@Wendall I am specifically interested in 64 bit integers.Santiagosantillan
Do you need something guaranteed 64 bit or are you okay with at least 64 bit support?Oder
@Oder 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?Santiagosantillan
I have no idea. My idea didn't pan out though so I guess it doesn't matter, sorry.Oder
@doynax Thanks. Should __builtin_clzll give the same answer whether you use long long (at least 64 bits) or int64_t (exactly 64 bits)?Santiagosantillan
@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.Radman
A
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.

Absolutely answered 15/6, 2017 at 18:49 Comment(5)
What date did g++/clang support long long/int64_t from?Santiagosantillan
@eleanora Since GCC 4.3 (aka March 5, 2008)Corrosion
@DavidÁlvarez Thanks. It looks like int64_t support came a little later than long long.Santiagosantillan
@eleanora Yes because long long int was added for C99 and int64_t was an optional C++11 featureCorrosion
@DavidÁlvarez Re: int64_t, it looks like gcc 4.5 added a builtin <stdint.h> for platforms where libc doesn't have one, but that was in 2010Santiagosantillan

© 2022 - 2024 — McMap. All rights reserved.