c++literalsint128

How to create a 128-bit integer literal?


I have an integer literal in the format 0x75f17d6b3588f843b13dea7c9c324e51. Is there a way to avoid the compiler syntax error "integer literal is too large to be represented in any integer type"?

Because I know I can work with those kinds of types (I'm using uint128_t from the EOS library and if I manually insert it, it works).

Is there a way to somehow parse this string directly into the exact same integer at run time?


Solution

  • 128 bit integer literals are not mandated by the standard, so it's up to the implementation if it wants to allow them. Most don't, so you'll need to break it up into two 64-bit components and use bitwise operators to combine them:

    __uint128_t num = ((__uint128_t)0x75f17d6b3588f843 << 64) | 0xb13dea7c9c324e51;
    

    A good compiler should perform the operations at compile time.