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

后端 未结 3 699
野性不改
野性不改 2020-12-21 05:18

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

相关标签:
3条回答
  • 2020-12-21 05:47

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

    0 讨论(0)
  • 2020-12-21 05:56

    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;
    
    0 讨论(0)
  • 2020-12-21 05:59

    Assuming a long is 64 bits on your platform,

    int v1 = 123;
    int v2 = 456;
    long val = v1 << 32 | v2;
    
    0 讨论(0)
提交回复
热议问题