Why does combining two shifts of a uint8_t produce a different result?

前端 未结 2 1003
逝去的感伤
逝去的感伤 2020-12-02 02:37

Could someone explain me why:

x = x << 1;
x = x >> 1;

and:

x = (x << 1) >> 1;

pr

2条回答
  •  清歌不尽
    2020-12-02 03:05

    When you bitshift the result is promoted to int. In the first example you convert int back to uint8_t everytime, and lose the intermediate data. But in the second example you keep the int result when you shift back.

提交回复
热议问题