Incosistent integer promotion when performing bitwise OR and leftshifting

40 Views Asked by At

I'm getting -Wconversion warning in C.

conversion from 'int' to 'uint8_t' {aka 'unsigned char'} may change value [-Wconversion]

When performing the following operation:

uint8_t output_A = 0x00;
uint8_t output_B = 0x00;
uint8_t example = 0x83;

output_A |= (uint8_t)((example & 0x01) << 7); /* No warning */
output_B |= (uint8_t)((example & 0x01) << 6); /* Warning */

uint8_t output_C = 0x00;
output_C |= (uint8_t)((example & 0x01) << 22); /* No warning */

Why is there warning for case B but not for case A? I would expect either warning for both or neither.

Why is there no warning in case C? In that case, the value gets promoted to int, before we cast it to uint8_t and lose information.

Using gcc 10.3.1.

0

There are 0 best solutions below