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

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

    Some examples:

    • Bit operations for example converting to and from Base64 (which is 6 bits instead of 8)
    • doing power of 2 operations (1 << 4 equal to 2^4 i.e. 16)
    • Writing more readable code when working with bits. For example, defining constants using 1 << 4 or 1 << 5 is more readable.

提交回复
热议问题