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

前端 未结 9 2157
傲寒
傲寒 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:39

    Left bit shifting to multiply by any power of two. Right bit shifting to divide by any power of two.

    x = x << 5; // Left shift
    y = y >> 5; // Right shift
    

    In C/C++ it can be written as,

    #include 
    
    x = x * pow(2, 5);
    y = y / pow(2, 5);
    

提交回复
热议问题