Can anyone explain why '>>2' shift means 'divided by 4' in C codes?

后端 未结 10 1434
谎友^
谎友^ 2020-12-05 09:25

I know and understand the result.

For example:


7 (decimal) = 00000111 (binary)
and 7 >> 2 = 00000001 (binary)
10条回答
  •  独厮守ぢ
    2020-12-05 09:45

    they do that because shifting is more efficient than actual division. you're just moving all the digits to the right or left, logically multiplying/dividing by 2 per shift

    If you're wondering why 7/4 = 1, that's because the rest of the result, (3/4) is truncated off so that it's an interger.

提交回复
热议问题