How to store a 64 bit integer in two 32 bit integers and convert back again

前端 未结 6 958
太阳男子
太阳男子 2020-11-30 02:09

I\'m pretty sure its just a matter of some bitwise operations, I\'m just not entirely sure of exactly what I should be doing, and all searches return back \"64 bit vs 32 bit

6条回答
  •  一整个雨季
    2020-11-30 02:25

    The basic method is as follows:

    uint64_t int64;
    uint32_t int32_1, int32_2;
    
    int32_1 = int64 & 0xFFFFFFFF;
    int32_2 = (int64 & (0xFFFFFFFF << 32) ) >> 32;
    
    // ...
    
    int64 = int32_1 | (int32_2 << 32);
    

    Note that your integers must be unsigned; or the operations are undefined.

提交回复
热议问题