Define a union data struct WORD_T for a uint16_t integer so that
a value can be assigned to a WORD_T integer in three ways:
(1) To assign the value to each bit of the integer,
(2) To assign the value to each byte of the integer,
(3) To assign the value to the integer directly.
a) Show the code of defining the union data struct WORD_T.
b) Show the code to assign 17 to the WORD_T variable foo.
b.1) Assign 17 to the bits of foo.
b.2) Assign 17 to the bytes of foo.
b.3) Assign 17 to the integer of foo.
A)
union WORD_T
{
uint16_t word;
struct
{
uint8_t byte0;
uint8_t byte1;
};
struct
{
uint8_t bit0:1;
uint8_t bit1:1;
uint8_t bit2:1;
uint8_t bit3:1;
uint8_t bit4:1;
uint8_t bit5:1;
uint8_t bit6:1;
uint8_t bit7:1;
uint8_t bit8:1;
uint8_t bit9:1;
uint8_t bit10:1;
uint8_t bit11:1;
uint8_t bit12:1;
uint8_t bit13:1;
uint8_t bit14:1;
uint8_t bit15:1;
};
};
B.1)
union WORD_T foo;
foo.bit0 = 1; // (1) Assign to individual bits
foo.bit1 = 0;
foo.bit2 = 0;
foo.bit3 = 0;
foo.bit4 = 1;
B.2)
foo.byte0 = 1; // (2) Assign to individual byte
foo.byte1 = 2;
B.3)
foo.word = 0x11; // (3) Assign to entire word
Get Answers For Free
Most questions answered within 1 hours.