3

I'm trying to refactor an old C++ code. At some point I've something like:

#if defined(WIN32) && !(defined(__CYGWIN__) || defined(__MINGW32__))
#  define I64_CONST(X)  X ## i64
#else
#  define I64_CONST(X)  X ## LL
#endif

So for defining a 64-bit literal in the code there's something like:

(uint32_t)((data_in >> 32) & I64_CONST(0x00000000ffffffff));

This is for using i64 suffix with Microsoft compilers and LL with other ones. Since I'm adapting it for C++17 and the minimum requirement that we have is to use Visual Studio 2019, is it possible to remove this and use LL everywhere, or is there some issue and it's better to maintain the distinction between compilers?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Jepessen
  • 11,744
  • 14
  • 82
  • 149
  • I think, for some time now, the C++ Standard has required `long long int` to be at least 64 bits wide. – Adrian Mole Jan 25 '22 at 15:18
  • 2
    @AdrianMole is correct. a `long long` is going to be at least 64 bits wide, but it could be wider. If you want a standard 64 bit wide type, use `std::int64_t` – NathanOliver Jan 25 '22 at 15:19

1 Answers1

7

Yes, long long is a new type since C++11 which contains at least 64 bits, so it can be used for your literals (unless the source code is using two's complement and the compiler is using one's complement/sign-magnitude then -263 won't fit)

Another way is to use the INT64_C in <cstdint> which was also introduced in C++11

#define I64_CONST INT64_C
auto v = INT64_C(0xFFFF0000FFFF0000);

See Purpose of using UINT64_C?

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 2
    There's really no need for macro's anymore. The target is VS2019/C++17. Any compiler which can handle that will also support C++11 `long long`. There's no need for 64 bit hacks in this century. – MSalters Jan 25 '22 at 15:48
  • @PaulSanders obviously not. It redefines a project macro to a standard macro to avoid modifying the whole code. But – phuclv Jan 25 '22 at 16:04
  • @MSalters of course it's not necessary. It's just a standard alternative – phuclv Jan 25 '22 at 16:05