If I have a structure like this:
struct example
{
uint16_t bar:1;
uint16_t foo:1;
};
Then I have another 16-bit variable: uint16_t value;
If I want the first bit in the "value" to be assigned to "example.bar" and the second bit in the value to be assigned to "example.foo", how would I do it?
EDIT:
I tried the following:
typedef struct
{
unsigned short in_1_16;
unsigned short in_17_32;
unsigned short in_33_48;
} READSTR;
typedef struct
{
unsigned short chan_1_16;
unsigned short chan_17_32;
unsigned short chan_33_48;
} TLM;
void main()
{
TLM tlm;
tlm.chan_1_16 = 0xFFFF;
READSTR t675;
t675.in_1_16 = 0x10;
t675.in_17_32 = 0x9;
t675.in_33_48 = 0x8;
tlm.chan_1_16 |= t675.in_1_16 & 1;
tlm.chan_1_16 |= t675.in_1_16 & (1 << 1);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 2);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 3);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 4);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 5);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 6);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 7);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 8);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 9);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 10);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 11);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 12);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 13);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 14);
tlm.chan_1_16 |= t675.in_1_16 & (1 << 15);
printf("%x", tlm.chan_1_16);
So, I set the value of in to struct for all (0xFFFF), and I'm trying to set it to 0x10, bit by bit. But when I run this code, I still get 0xFFFF. Not sure what I am doing wrong?
source
share