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.
source share