Answer the question
In order to leave comments, you need to log in
C++: Anonymous structures in union?
In light of the recent articles about changing the C++11 standards, I wanted to ask the following point. I'm not a great C++ expert, I'm still learning. Faced the following problem, the device transmits information about its state in one byte, where each specific bit corresponds to a specific parameter. There are very handy things in C/C++ - union and bit fields.
In theory, for such a task, it should look like this
union Data
{
struct
{
unsigned int param1 :1;
unsigned int param2 :1;
unsigned int param3 :1;
unsigned int param4 :1;
unsigned int param5 :1;
unsigned int param6 :1;
unsigned int param7 :1;
unsigned int param8 :1;
} params;
unsigned char Byte;
};
Data info;
info.Byte = 0x21;
info.params.param3 = 1;
union Data
{
struct
{
unsigned int param1 :1;
unsigned int param2 :1;
unsigned int param3 :1;
unsigned int param4 :1;
unsigned int param5 :1;
unsigned int param6 :1;
unsigned int param7 :1;
unsigned int param8 :1;
};
unsigned char Byte;
};
Data info;
info.Byte = 0x21;
info.param3 = 1;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question