How could one "split" i.e. long long type, so that first part of it is in var of type long and second part is also in var of type long.
long long long_type = 0xaaaabbbbccccdddd;
and in first int I would like to have first half(doesn't matter from what side) of the long_type var and in second var int second half.
Your question is ambiguous. It depends on what you mean by "split". You can split the value representation of your original long long or you can split the object representation of your long long.
If you want to split the value representation, then your question is even more ambiguous due to the fact that your original value is signed. How do you intend to split a signed value? What kind of result do you expect? Signed? Unsigned? High-order part signed, low-order part unsigned? Or something else?
For an unsigned value it would look as follows (assuming that the recipient type long has the right size for your purposes)
unsigned long long long_type = ...;
unsigned long hi = long_type / ULONG_MAX;
unsigned long lo = long_type;
If you want to split the object representation, the proper way to do it would be to use memcpy (in this case the signedness of the original value is not of importance)
long long long_type = ...;
unsigned long hi, lo;
memcpy(&lo, &long_type, sizeof lo);
memcpy(&hi, (char *) &long_type + sizeof lo, sizeof hi);
In this case, of course, which part is actually the low-order one and which part is the high-order one will depend on the platform.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With