I have fed the following code through a static analysis tool:
u1 = (u1 ^ u2); // OK
u1 = (u1 ^ u2) & u3; // NOT OK
u1 = (u1 ^ u2) & 10; // NOT OK
u1 = (u1 ^ u2) & 10U; // NOT OK
u1 = (unsigned char)(u1 ^ u2) & 10U; // OK
u1 = (unsigned char)(u1 ^ u2) & u3; // OK
“OK” means the static analysis tool did not complain.
“NOT OK” means the static analysis tool did complain — claiming that some operand of a bitwise operation is not an unsigned integer.
The results from the last 2 lines show that the parentheses are causing either
a. an actual type conversion to signed
b. something that the static analysis tool thinks is a type conversion to signed
I will ask the static analysis tool developer about (b).
But before I do, I would like to know if perhaps the C language is known to do (a)?
Nothing in C is done below
int: eg when adding twounsigned chars, even before the addition, the operands are converted tointaccording to the default promotions.In the last line, first
u1andu2are converted toint, then the+operator is applied to obtain aintvalue and then that value is converted back tounsigned char(of course the compiler can use shortcuts!)