Why does it make a difference if left and right shift are used together in one expression or not?

后端 未结 3 1243
栀梦
栀梦 2020-12-29 19:14

I have the following code:

unsigned char x = 255;
printf(\"%x\\n\", x); // ff

unsigned char tmp = x << 7;
unsign         


        
3条回答
  •  伪装坚强ぢ
    2020-12-29 19:21

    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.

提交回复
热议问题