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".
我很确定它只是一些按位操作的问题,我只是不完全确定我应该做什么,并且所有搜索都返回“64位对32位”。
5 个解决方案
#1
33
pack:
包:
u32 x, y;
u64 v = ((u64)x) << 32 | y;
unpack:
解压:
x = (u32)((v & 0xFFFFFFFF00000000LL) >> 32);
y = (u32)(v & 0xFFFFFFFFLL);
#2
9
Or this, if you're not interested in what the two 32-bits numbers mean:
或者,如果您对两个32位数字的含义不感兴趣:
u32 x[2];
u64 z;
memcpy(x,&z,sizeof(z));
memcpy(&z,x,sizeof(z));
#3
6
Use a union and get rid of the bit-operations:
使用联合并摆脱位操作:
<stdint.h> // for int32_t, int64_t
union {
int64_t big;
struct {
int32_t x;
int32_t y;
};
};
assert(&y == &x + sizeof(x));
simple as that. big consists of both x and y.
就那么简单。 big由x和y组成。
#4
2
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.
请注意,您的整数必须是未签名的;或者操作未定义。
#5
2
I don't know if this is any better than the union or memcpy solutions, but I had to unpack/pack signed 64bit integers and didn't really want to mask or shift anything, so I ended up simply treating the 64bit value as two 32bit values and assign them directly like so:
我不知道这是否比union或memcpy解决方案更好,但是我不得不解压缩/打包有符号的64位整数并且不想掩盖或转移任何东西,所以我最终只是将64位值视为两个32位值并直接分配它们,如下所示:
#include <stdio.h>
#include <stdint.h>
void repack(int64_t in)
{
int32_t a, b;
printf("input: %016llx\n", (long long int) in);
a = ((int32_t *) &in)[0];
b = ((int32_t *) &in)[1];
printf("unpacked: %08x %08x\n", b, a);
((int32_t *) &in)[0] = a;
((int32_t *) &in)[1] = b;
printf("repacked: %016llx\n\n", (long long int) in);
}
#1
33
pack:
包:
u32 x, y;
u64 v = ((u64)x) << 32 | y;
unpack:
解压:
x = (u32)((v & 0xFFFFFFFF00000000LL) >> 32);
y = (u32)(v & 0xFFFFFFFFLL);
#2
9
Or this, if you're not interested in what the two 32-bits numbers mean:
或者,如果您对两个32位数字的含义不感兴趣:
u32 x[2];
u64 z;
memcpy(x,&z,sizeof(z));
memcpy(&z,x,sizeof(z));
#3
6
Use a union and get rid of the bit-operations:
使用联合并摆脱位操作:
<stdint.h> // for int32_t, int64_t
union {
int64_t big;
struct {
int32_t x;
int32_t y;
};
};
assert(&y == &x + sizeof(x));
simple as that. big consists of both x and y.
就那么简单。 big由x和y组成。
#4
2
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.
请注意,您的整数必须是未签名的;或者操作未定义。
#5
2
I don't know if this is any better than the union or memcpy solutions, but I had to unpack/pack signed 64bit integers and didn't really want to mask or shift anything, so I ended up simply treating the 64bit value as two 32bit values and assign them directly like so:
我不知道这是否比union或memcpy解决方案更好,但是我不得不解压缩/打包有符号的64位整数并且不想掩盖或转移任何东西,所以我最终只是将64位值视为两个32位值并直接分配它们,如下所示:
#include <stdio.h>
#include <stdint.h>
void repack(int64_t in)
{
int32_t a, b;
printf("input: %016llx\n", (long long int) in);
a = ((int32_t *) &in)[0];
b = ((int32_t *) &in)[1];
printf("unpacked: %08x %08x\n", b, a);
((int32_t *) &in)[0] = a;
((int32_t *) &in)[1] = b;
printf("repacked: %016llx\n\n", (long long int) in);
}