Let's take for example the following two 1-byte variables:
uint8_t x1 = 0x00;
uint8_t x2 = 0xFF;
When printing the bitwise complement, the result is a 4-byte variable:
printf("%02X -> %02X; %02X -> %02X\n", x1, ~x1, x2, ~x2);
00 -> FFFFFFFF; FF -> FFFFFF00
I know this can be "solved" using casting or masking:
printf("%02X -> %02X; %02X -> %02X\n", x1, (uint8_t) ~x1, x2, (uint8_t) ~x2);
00 -> FF; FF -> 00
printf("%02X -> %02X; %02X -> %02X\n", x1, ~x1&0xFF, x2, ~x2&0xFF);
00 -> FF; FF -> 00
But why the non-intuitive behavior in the first place?
%Xis forunsigned int. And no,uint8_tis not a 2-byte variable.