My question looks a lot like
How to store a 64 bit integer in two 32 bit integers and convert back again
(I have an unsigned 32 bit I need to put into 4 unsigned 8-bit variables in C)
but
My question is whether this:
uint8_t a;
uint32_t b;
a = b;
guarantees that a is filled with the eight rightmost bits, rather than the eight leftmost bits?
Yes. Do either this:
uint32_t num32 = 0xdeadbeef;
uint8_t a = num32; /* 0xef */
uint8_t b = num32 >> 8; /* 0xbe */
uint8_t c = num32 >> 16; /* 0xad */
uint8_t d = num32 >> 24; /* 0xde */
Or this:
union u
{
uint32_t num32;
struct
{
uint8_t a;
uint8_t b;
uint8_t c;
uint8_t d;
} bytes;
} converter;
converter.num32 = 0xdeadbeef;
The first example does not depend on platform endianess, the second does.
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