-
long int to int
Hi,
I have a long int:
unsigned long int x = 0xFFFFFFFFAAAAAAAA;
I want to get
unsigned int low;
unsigned int high;
such that
low = 0xAAAAAAAA
high = 0xFFFFFFFF
I tried a bit shift and it doesn't work with MSVC but it does work with RVCT (ARM compiler). Is there a standard lib function I can use?
Thanks
-
What does your actual code look like?
How about something like this:
Code:
union u
{
unsigned long long ll;
struct two_int
{
unsigned high;
unsigned low;
};
};
...
u.ll = longValue;
cout << u.high;
cout << u.low;
...
Edit: The order of high/low depends on whether your machine is big or little endian, so you may need to swap them.
--
Mats
-
Apart from endianness, there is also the problem that int (signed or unsigned) is the same size as long with some compilers. MSVC is one of those.
Make low and high of type unsigned short, rather than unsigned int. And, preferably, confirm that sizeof(unsigned long) == sizeof(unsigned short)*2.
-
Grumpy, in principle I agree. But the resolution seems a bit wrong:
The values shown indicate a 64-bit "long" and a 32-bit "int", so if that's what the original poster wants, then we would be looking at using a 64-bit type for the "long" value (e.g. long long in my example), and a unsigned int for the 32-bit value.
--
Mats