An enumeration constant such as those in this example are always equivalent to int
. Since int
is always signed, you cannot use enumeration constants in bitwise expressions, unless you use an explicit typecast.
As has already been mentioned, replacing | with + will do the trick. Alternatively (though less readable), you could have written
gbrALL = (int)( (uint16_t)gbrLEFT |
(uint16_t)gbrTOP |
(uint16_t)gbrRIGHT |
(uint16_t)gbrBOTTOM)
It would have been MISRA-C compilant but very ugly code...
The best and easiest way to write MISRA-C conformant code is to avoid enums entirely. Not only are enumeration constants always equal to int
. But an enumerated type (a variable with enum type) has implementation-defined size and signedness (C11 6.7.2.2). Perhaps you thought that C was a rational language and that an enumerated type would be of a type always suitable to hold an enumeration constant. Not so. It could be unsigned, it could be a small integer type or a large one. This is one of many big flaws in the C language, a flaw that MISRA-C will painfully expose if you weren't already aware.
Even more painfully, MISRA-C enforces you to document all implementation-defined behavior relied upon. Meaning that you can't just pass static analysis to make the code containing enums MISRA-C compliant. You'll also have to write an essay of all the oddities and flaws of the enum type and state how enum is implemented on your specific compiler, by citing compiler documentation. (And if the compiler lacks such documentation, it isn't C compliant and therefore causes further MISRA issues).
Therefore, the best way to achieve what you want is to #define
all constants as unsigned integer literals:
#define gbrLEFT 0x01u
and so on.