假设我有一个长整型,想要取它的位,并用它构造四个无符号的短整型。
在这里,特定的顺序并不重要。
我通常知道我需要移位并截断到无符号短整型的大小。但我想我可能在什么地方犯了个奇怪的错误,所以我问了。
发布于 2008-09-29 10:44:59
#include <stdint.h>
#include <stdio.h>
union ui64 {
uint64_t one;
uint16_t four[4];
};
int
main()
{
union ui64 number = {0x123456789abcdef0};
printf("%x %x %x %x\n", number.four[0], number.four[1],
number.four[2], number.four[3]);
return 0;
}发布于 2008-09-29 10:48:10
(unsigned short)((((unsigned long long int)value)>>(x))&(0xFFFF))其中value是您的long long int,x是0、16、32或48表示四个短片。
发布于 2008-09-29 10:45:57
union LongLongIntToThreeUnsignedShorts {
long long int long_long_int;
unsigned short int short_ints[sizeof(long long int) / sizeof(short int)];
};这应该可以做你想做的事情,而不需要浪费在位移位上。
https://stackoverflow.com/questions/148225
复制相似问题