Si j'ai un struct comme ceci:Attribution d'un bit de valeur par bit
struct example
{
uint16_t bar:1;
uint16_t foo:1;
};
, j'ai une autre variable 16 bits: uint16_t value;
Si je veux que le premier bit « valeur » pour être assigné à "example.bar", et le second bit en valeur à affecter à "example.foo", comment ferais-je cela?
EDIT:
J'ai essayé les éléments suivants:
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);
Je suis en train de la valeur sur struct à tous ceux (0xFFFF), et je suis en train de le mettre à 0x10, peu par bit. Mais quand je cours ce code j'obtiens toujours 0xFFFF. Vous ne savez pas ce que je fais mal?
Comment définissez-vous "first bit in" value ""? Le premier bit est-il le bit le plus significatif ou le bit le moins significatif? – oosterwal
'uint16'? Qu'est-ce que c'est? Voulez-vous dire 'uint16_t'? –
@Chris oui, je voulais dire uint16_t – Blade3