Since it's been a really long time since I've done one, I figure its time to write a programming tip. The C++ bit fields is a nice little C++ trick to limit the size of data type. Now it may not be apparent but this is a super easy way to build a structure with variable size data bits.
So lets say you have a message packet that has 8 different flags that is 1 byte a piece, normally you'd probably do some sort of bit shift operation and some masking. However, you can skip all that nonsense and write something like this
struct Flags{
unsigned short A:1;
unsigned short B:1;
unsigned short C:1;
unsigned short D:1;
unsigned short E:1;
unsigned short F:1;
unsigned short G:1;
unsigned short H:1;
};
Thus allowing you to use a nice well defined structure rather than doing all that unnecessary bit shifting. The bite field operator is used in conjunction with a variable definition by using the colon operator (Example int test: 5;) this example specifies a signed variable that has a 5 bit length.
No comments:
Post a Comment