C未运营商为什么我得到一个警告

问题描述:

请告诉我错这个code

Whats 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 

编译器是IAR EWARM
为什么要两个char变量需要转换为int。为什么要抱怨标志的改变,当一切都被宣布为无符号。

Compiler is IAR EWARM why should two char variables need to be converted to an int. why should it complain about a change of sign when everything is declared as unsigned.

任何想法转换为使用摆脱这种警告?

Any idea what cast to use to get rid of this warning?

C的规则要求 unsigned char型的操作数转换为 INT (除了不正当C实现)。

The rules of C require that unsigned char operands be converted to int (except in perverse C implementations).

一旦操作数是 INT ,它被签署,而运营商可能会给你意想不到的效果,因为有符号整数和位重新presentations语义不被完全C.指定的编译器警告有益你这个问题。

Once the operand is an int, it is signed, and the ~ operator may give you unexpected results, because the semantics for signed integers and their bit representations are not fully specified by C. The compiler is helpfully warning you about this.

您应该使用反模式=〜(无符号整数)模式; 。随着 unsigned int类型,你保证该值是简单的二元psented重新$ P $。

You should use antipattern = ~ (unsigned int) pattern;. With unsigned int, you are guaranteed that the value is represented with simple binary.