What do >> and << mean in Python?

后端 未结 7 1828
无人共我
无人共我 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 10:05

    These are bitwise shift operators.

    Quoting from the docs:

    x << y
    

    Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.

    x >> y
    

    Returns x with the bits shifted to the right by y places. This is the same as dividing x by 2**y.

提交回复
热议问题