I have the following variables.
uint8_t flags;
uint32_t token;
I need to write function that cobine them into one uint64_t, and than parse them back into two variables, one of uint8_t and one of uint32_t.
uint64 convert(uint8_t flags, uint32_t token);
void convertBack(uint64 allTogether, uint8_t* flags, uint32_t* token);
I tried to found something that doing the following, but most of what I found is convert two of the same to one bigger, like two uint32_t to one uint64_t
Thank You
How about:
uint64_t convert(uint8_t flags, uint32_t token)
{
return ((uint64_t) flags << 32) | token;
}
This puts the 8-bit field "above" the 32-bit one, i.e. the returned uint64_t
has its bits arranged like this:
+---------------------------------+--------------------------------+
|666655555555554444444444|33333333|3322222222221111111111 |
|321098765432109876543210|98765432|10987654321098765432109876543210|
+------------------------+--------+--------------------------------+
| unused(24) |flags(8)| token(32) |
+------------------------+-----------------------------------------+
Bit numbers in decimal should be read downwards, bit 0 is on the far right (the LSB).
I'll leave convertBack()
(which is a pretty bad name, both of these names are sub-optimal) to you. :)