gcc 7.3 128-bit unsigned integer operation

穿精又带淫゛_ 提交于 2020-06-17 10:28:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!