Accessing lower half of a 64bit integer
How can access the lower half of a 64bit integer using C or C++? I can do it easily in Assembly but I have no clue on how to do it in C/C++
EDIT: Wha开发者_如何学Got about accessing the upper half?
long long BigOne = 0x1234567890ABCDEFLL;
long long LowerHalf = BigOne & 0xFFFFFFFFLL;
long long UpperHalf = (BigOne >> 32) & 0xFFFFFFFFLL;
Sorry if the hexadecimal literals require some prefix/suffix, I'm not very familiar with C. Please fix the answer if you know.
I have used all kinds of tricks to do this before. Using unions, long shifts ect..
Nowadays I just use memcopy. It may sound inefficient, but last time I checked the compiler optimized the code quite nice:
int32_t higher32 (unsigned long long arg)
{
unsigned char * data = (unsigned char *) arg;
int32_t result;
memcpy (&result, data+4, sizeof (int32_t));
return result;
}
int32_t lower32 (unsigned long long arg)
{
unsigned char * data = (unsigned char *) arg;
int32_t result;
memcpy (&result, data+0, sizeof (int32_t));
return result;
}
精彩评论