Can brackets in C change the type of the result of the operands of a bitwise operation?

I passed 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 that the static analysis tool complained - claiming that some operand of the bitwise operation is not an unsigned integer.

The results of the last two lines show that parentheses call either

but. actual type to signed conversion

b. what the static analysis tool considers to be a type to signed conversion

I will ask the developer of the static analysis tool about (b).

But before I do this, I would like to know if C (a) seems to be able to?

+5
source share
2 answers

Nothing in C is executed below int : for example, when two unsigned chars are added, even before being added, the operands are converted to int according to the default values.

 unsigned char u1, u2, u3; u1 = 0; u2 = 42; u3 = u1 + u2; 

In the last line, the first u1 and u2 converted to int , then the + operator is used to get the int value, and then this value is converted back to unsigned char (of course, the compiler can use shortcuts!)

+6
source

This is because in C the resulting operation type on two unsigned char : s is int . The static analysis tool correctly (though not very intuitively) reports that & applies to int .

+3
source

All Articles