24.3.13 Default Bit-field int
Type
The type of a bit-field specified as a plain int
is identical to that of
one defined using unsigned int
. This is quite different from other
objects where the types int
, signed
and signed
int
are synonymous. It is recommended that the signedness of the bit-field
be explicitly stated in all bit-field definitions.
Example
The following shows an example of a structure tag containing bit-fields that are unsigned integers and with the size specified.
struct OUTPUTS {
int direction :1;
int parity :3;
int value :4;
};
Differences
When targeting PIC devices, the MPLAB XC8 compiler has issued a warning if an
int
type was specified for bit-fields, and it has instead
implemented the bit-field with an unsigned int
type. For other devices,
this compiler has implemented bit-fields defined using int
as having a
signed int
type, unless the option
-funsigned-bitfields
was specified. Use of a signed
int
type is also true for the MPLAB XC16, XC-DSC, and XC32 compilers.
Migration to the CCI
Any code that defines a bit-field with the plain int
type should be
reviewed. If the intention was for these to be signed quantities, then the type of these
should be changed to signed int
. In the following example:
struct WAYPT {
int log :3;
int direction :4;
};
the bit-field type should be changed to signed int
, as in:
struct WAYPT {
signed int log :3;
signed int direction :4;
};