[英]How to extract four unsigned short ints from one long long int?
Suppose I have one long long int and want to take its bits and construct four unsigned short ints out of it. 假设我有一个long long int并且想要取其位并构造四个无符号短整数。
Particular order doesn't matter much here. 特别顺序在这里并不重要。
I generally know that I need to shift bits and truncate to the size of unsigned short int. 我通常知道我需要将位移位并截断为unsigned short int的大小。 But I think I may make some weird mistake somewhere, so I ask. 但我想我可能会在某个地方犯一些奇怪的错误,所以我问。
#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;
}
(unsigned short)((((unsigned long long int)value)>>(x))&(0xFFFF))
其中value
是long long int
,对于四个short, x
是0,16,32或48。
union LongLongIntToThreeUnsignedShorts {
long long int long_long_int;
unsigned short int short_ints[sizeof(long long int) / sizeof(short int)];
};
That should do what you are thinking about, without having to mess around with bit shifting. 这应该做你正在考虑的事情,而不必乱用比特移位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.