C is not an operator, why am I getting a warning

What is wrong with this code

typedef unsigned char datum; /* Set the data bus width to 8 bits. */ datum pattern; datum antipattern; antipattern = ~pattern; Remark[Pa091]: operator operates on value promoted to int (with possibly unexpected result) C:\filepath...\file.c 386 

The compiler is IAR EWARM why two char variables must be converted to int. why should he complain about a sign change when everything is declared unsigned.

What idea can be used to get rid of this warning?

+4
source share
1 answer

C rules require unsigned char operands to be converted to int (with the exception of child C implementations).

As soon as the operand is an int , it is signed, and the ~ operator can give you unexpected results, because the semantics for signed integers and their bit representations are not completely defined C. The compiler helps you about this.

You should use antipattern = ~ (unsigned int) pattern; . With unsigned int you are guaranteed that this value is represented by simple binary code.

+4
source

All Articles