How is shift operator evaluated in C?

后端 未结 3 1764
清歌不尽
清歌不尽 2021-01-01 11:53

I recently noticed a (weird) behavior when I conducted operations using shift >> <<!

To explain it, let me write this small run

3条回答
  •  粉色の甜心
    2021-01-01 12:17

    In the first example:

    • a is converted to an int, shifted left, then right and then converted back to usigned char.

    This will result to a=5 obviously.

    In the second example:

    • b is converted to int, shifted left, then converted back to unsigned char.
    • b is converted to int, shifted right, then converted back to unsigned char.

    The difference is that you lose information in the second example during the conversion to unsigned char

提交回复
热议问题