开发者

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

开发者 https://www.devze.com 2023-03-27 16:05 出处:网络
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 tha开发者

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 tha开发者_如何学Got done?


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;


Assuming a long is 64 bits on your platform,

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号