You may write a user defined literal (since C++11) for 128-bit integers.
A raw literal operator takes a single const char*
as parameter. You can than write a function body to parse the string.
For example:
// Use __uint128_t for demonstration.
constexpr __uint128_t operator""_uint128_t(const char* x)
{
__uint128_t y = 0;
for (int i = 2; x[i] != '\0'; ++i)
{
y *= 16ull;
if ('0' <= x[i] && x[i] <= '9')
y += x[i] - '0';
else if ('A' <= x[i] && x[i] <= 'F')
y += x[i] - 'A' + 10;
else if ('a' <= x[i] && x[i] <= 'f')
y += x[i] - 'a' + 10;
}
return y;
}
Obviously, this implementation is problematic because I'm too lazy to develop a full solution, it only support hexadecimal, it does not check the 0x
prefix, etc. And it requires the C++14 relaxed constexpr
function. But it demonstrates that you can actually parse this string directly into the exact same integer.
Let's test it out:
int main()
{
auto abc = 0x1234567890ABCDEFfedcba0987654321_uint128_t;
std::uint64_t higher = abc >> 64;
std::uint64_t lower = abc;
std::cout << std::hex << higher << ' ' << lower;
}
http://coliru.stacked-crooked.com/a/fec4fc0fd4ff1418