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

后端 未结 3 1259
栀梦
栀梦 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条回答
  •  -上瘾入骨i
    2020-12-29 19:25

    The shift operator is not defined for the char types. The value of any char operand is converted to int and the result of the expression is converted the char type. So, when you put the left and right shift operators in the same expression the calculation will be performed as type int (without loosing any bit), and the result will be converted to char.

提交回复
热议问题