I am using MISRA C 2004 standards in Code Composer Studio. I am always getting errors with respect to bitwise operations during initialization of the peripherals.
In the compiler .h file, the definition is like this.
#define SFR_8BIT(addr) extern volatile unsigned char addr
SFR_8BIT(REG1);
#define REG2 REG1
Now in my code, if I use
REG2 |= 0x01;
The MISRA C is popping out these errors:
Bitwise operators shall not be applied to operands whose underlying type is signed - #1393-D (MISRA-C:2004 10.1/R)
The value of an expression of integer type shall not be implicitly converted to a different underlying type if it is not a conversion to a integer type of the same signedness.
I don't want to change the compiler .h file, and I want to eradicate these warnings.
At a guess, your char
is 8 bits and int
is (at least) 16. That means all values of unsigned char
can be represented as (signed) int
s. That, in turn, means in your expression REG2 |= 0x01;
, your unsigned char
will be promoted to int
, then the OR
operation carried out, then the result of that cast back to unsigned char
.
If I'm not mistaken, changing your constant to an unsigned char
should prevent that:
REG2 |= (unsigned char)0x01;
or:
unsigned char one = (unsigned char)0x01;
REG2 |= one;