C not operator why do I get a warning

后端 未结 1 898
星月不相逢
星月不相逢 2021-01-19 07:11

Whats wrong with this code

typedef unsigned char datum; /* Set the data bus width to 8 bits. */

    datum pattern;
    datum antipattern;

    antipattern =         


        
1条回答
  •  有刺的猬
    2021-01-19 07:38

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

    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.

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

    0 讨论(0)
提交回复
热议问题