If you want to send them in an 8-bit
field, allow for values between 0-3 for each enum
, I would do something like this:
#include <iostream>
#include <cstdint>
struct s_enum_t
{
s_enum_t(
int x,
int y,
int z)
: x_(x)
, y_(y)
, z_(z)
{}
union
{
struct
{
uint8_t x_ : 2;
uint8_t y_ : 2;
uint8_t z_ : 2;
uint8_t spare_ : 2;
};
uint8_t val;
};
};
int main()
{
s_enum_t v(1, 1, 1);
std::cout << v.val << "\n";
}
That will get the values laid out in a very specific manner. When sending between different systems you have to be aware of the endian scheme for each system though.
To clarify, with each individual enumerated value laid out in 2-bits
, it becomes easier to troubleshoot issues between the sending and receiving systems. In this particular case, you have to be familiar with how this will be laid out on each system (by the compiler) to know how to properly decode the values. Having individual values in a bitfield that can be printed out (for debugging purposes) allows you to easily identify which fields are which when doing the initial system integration testing.