I want to create a union in which the biggest member is a 32 bit integer. This is what will be mainly written to. Then there are four 8 bit variables, probably char types that will each refer to a different section of the 32 bit integer kind of like:
union {
int32 myint;
char char1 [7:0];
char char2 [15:8];
char char3 [23:16];
char char4 [31:24];
}
But I am not sure how to do this in C++.
This may work:
union {
int32 myint;
char chars[4];
};
I didn't understand if you wanted one 32bits interger AND 4 8bits variables or one 32bits interger split in 4 8bits variables, but anyway you should try something like this :
union yourUnion {
int32 yourInt;
struct {
int32 var1 : 8;
int32 var2 : 8;
int32 var3 : 8;
int32 var4 : 8;
} yourSplitInterger;
};
Hope it helps.
You can use this:
union myUnion {
int32 myint;
struct {
char char1;
char char2;
char char3;
char char4;
} myChars;
};
or with uint8_t
:
union myUnion {
uint32_t myint;
struct {
uint8_t b1;
uint8_t b2;
uint8_t b3;
uint8_t b4; // or number them in reverse order
} myBytes;
};
See here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With