What does a bitwise shift (left or right) do and what is it used for?

前端 未结 9 2154
傲寒
傲寒 2020-12-02 05:00

I\'ve seen the operators >> and << in various code that I\'ve looked at (none of which I actually understood), but I\'m just wondering

9条回答
  •  渐次进展
    2020-12-02 05:30

    Yes, I think performance-wise you might find a difference as bitwise left and right shift operations can be performed with a complexity of o(1) with a huge data set.

    For example, calculating the power of 2 ^ n:

    int value = 1;
    while (exponent

    Similar code with a bitwise left shift operation would be like:

    value = 1 << n;
    

    Moreover, performing a bit-wise operation is like exacting a replica of user level mathematical operations (which is the final machine level instructions processed by the microcontroller and processor).

提交回复
热议问题