问题
I'm confused with the usage of 128-bit integer. Please look at the test code:
uint128_t test_data = 0x00000000FFFFFFFF0101010101010101;
uint64_t test_data_h = test_data >> 64;
uint64_t test_data_l = test_data ;
printf("test_data 0x %016llx %016llx\n", test_data_h, test_data_l);
I expect the test_data_h
to be 0x00000000FFFFFFFF, but the result is :
test data 0x 0000000000000000 0101010101010101
Why is this?
回答1:
Many compilers do not support 128 bit constants.
Instead
// uint128_t test_data = 0x00000000FFFFFFFF0101010101010101;
uint128_t test_data = (((uint128_t) 0x00000000FFFFFFFF) << 64) | 0x0101010101010101;
Tip: enable all compiler warnings.
回答2:
GCC doesn't support 128-bit integer literal
There is no support in GCC for expressing an integer constant of type __int128 for targets with long long integer less than 128 bits wide.
128-bit Integers
So you'll have to construct it from smaller parts (high << 64) | low
来源:https://stackoverflow.com/questions/60860827/gcc-7-3-128-bit-unsigned-integer-operation