I have the following code:
unsigned char x = 255;
printf(\"%x\\n\", x); // ff
unsigned char tmp = x << 7;
unsign
The 'intermediate' values in your last case are (full) integers, so the bits that are shifted 'out of range' of the original unsigned char
type are retained, and thus they are still set when the result is converted back to a single byte.
From this C11 Draft Standard:
6.5.7 Bitwise shift operators
...
3 The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand ...
However, in your first case, unsigned char tmp = x << 7;
, the tmp
loses the six 'high' bits when the resultant 'full' integer is converted (i.e. truncated) back to a single byte, giving a value of 0x80
; when this is then right-shifted in unsigned char y = tmp >> 7;
, the result is (as expected) 0x01
.