I have the following structure:
struct
{
int a:4;
int b:7;
int c:21;
} example;
I would like to combine a and b to form an integer d in C++. For instance, I would like the bit values of a to be on the left of the bit values of b in order to form integer d. How is this implemented in c++?
Example:
a= 1001
b = 1010101
I would like int d = 10011010101 xxxxxxxxxxxxxxxxxxxxx
where x can be 21 bits that belonged to d previously. I would like the values of a and b to be put in bit positions 0-3 and 4-10 respectively since a occupies the first 4 bits and b occupies the next 7 bits in the struct “example”.
The part that I am confused about is that variable a and variable b both have a “sign” bit at the most significant bit. Does this affect the outcome? Are all bits in variable a and variable b used in the end result for integer d? Will integer d look like a concatenation of variable a’s bits and variable b’s bits?
Thanks
Note that whether an
intbit-field is signed or unsigned is implementation-defined. The C++ standard says this, and the C standard achieves the same net result with different wording:ISO/IEC 14882:2011 — C++
ISO/IEC 9899:2011 — C
The context of §6.7.2 shows that
intcan be combined withshort,longetc and the rule will apply; C++ specifies that a bit more clearly. The signedness of plaincharis implementation-defined already, of course.Unsigned bit-fields
If the type of the bit-fields are unsigned, then the expression is fairly straight-forward:
Signed bit-fields
If the values are signed, then you have a major interpretation exercise to undertake, deciding what the value should be if
example.ais negative andexample.bis positive, or vice versa. To some extent, the problem arises even if the values are both negative or both positive.Suppose
example.a = 7;andexample.b = 12;— what should be the value ofd? Probably the same expression applies, but you could argue that it would be better to shift by 1 fewer places:The other cases are left for you to decide; it depends on the interpretation you want to place on the values. For example:
This forces the signed values to be treated as unsigned. It probably isn’t what you’re after.
Modified question
For this to work, then you need:
You probably can use fewer parentheses; I’m not sure I’d risk doing so.
Union
However, with this revised specification, you might well be tempted to look at a
unionsuch as:However, the layout of the bits in the bit-fields w.r.t the bits in the
int x;is not specified (they could be most significant bits first or least significant bits first), and there are always mutterings about ‘if you access a value in a union that wasn’t the last one assigned to you invoke undefined behaviour’. Thus you have multiple platform-defined aspects of the bit field to deal with. In fact, this sort of conundrum generally means that bit-fields are closely tied to one specific type of machine (CPU) and compiler and operating system. They are very, very non-portable at the level of detail you’re after.