Is it possible to store 2 32-bit values in one long int variable?

白昼怎懂夜的黑 提交于 2019-12-01 19:08:52

问题


I want to store two 32-bit values in a single long int variable.

How would you do this on a 32-bit OS using C? Is it possible to store the data in a single long long variable? If so, how is that done?


回答1:


Assuming a long is 64 bits on your platform,

int v1 = 123;
int v2 = 456;
long val = v1 << 32 | v2;



回答2:


Use an uint64_t and bitwise operators.

uint64_t i64;
uint32_t a32, b32;

// Be carefull when shifting the a32.
// It must be converted to a 64 bit value or you will loose the bits
// during the shift. 
i64 = ((uint64_t)a32 << 32) | b32;



回答3:


Unless sizeof(long int) == 8, the answer is no. If that equality is true, then use Kevin's or cnicutar's answer.



来源:https://stackoverflow.com/questions/7027495/is-it-possible-to-store-2-32-bit-values-in-one-long-int-variable

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