What do >> and << mean in Python?

后端 未结 7 1810
无人共我
无人共我 2020-12-04 09:44

I notice that I can do things like 2 << 5 to get 64 and 1000 >> 2 to get 250.

Also I can use >> in pri

7条回答
  •  遥遥无期
    2020-12-04 09:49

    They are bit shift operator which exists in many mainstream programming languages, << is the left shift and >> is the right shift, they can be demonstrated as the following table, assume an integer only take 1 byte in memory.

    | operate | bit value | octal value |                       description                        |
    | ------- | --------- | ----------- | -------------------------------------------------------- |
    |         | 00000100  |           4 |                                                          |
    | 4 << 2  | 00010000  |          16 | move all bits to left 2 bits, filled with 0 at the right |
    | 16 >> 2 | 00000100  |           4 | move all bits to right 2 bits, filled with 0 at the left |
    

提交回复
热议问题