I have the following issue using an ARM® Cortex™-M4F CPU running mbedOS 5.9.
Say I have the binary value 10101000
and that I also have the following union/struct:
union InputWord_u
{
uint8_t all;
struct BitField_s
{
uint8_t start : 1; // D7
uint8_t select : 3; // D6, D5, D4
uint8_t payload : 4; // D3, D2, D1, D0
} bits;
};
I have a simple program where I access my word and assign the values as such:
InputWord_u word;
word.bits.start = 0b1;
word.bits.select = 0b010;
word.bits.payload = 0b1000;
Therefore, word.all == 10101000
and is a uint8_t
.
If I print this as such printf("%u", word.all);
then I receive the value of 133
.
If I then define the following uint8_t
:
uint8_t value = 0b10101000;
And print this using printf("%u", value);
then I receive the value 168
.
I expect both values to equal 168.
I appreciate that this is likely me grossly misunderstanding how a Struct is represented in memory. Nevertheless, could someone please explain what is exactly going on?
Thanks.