What does << represent in python?

后端 未结 4 1686
醉酒成梦
醉酒成梦 2021-01-18 13:06

Python says

1 << 16 = 65536

What operation does << performs in Python?

相关标签:
4条回答
  • 2021-01-18 13:58

    It is the left shift operator for Python. A left shift operation, as the name says, move bits to the left.

    Suppose you have 2 whose binary representation is 0010. So 2<<2 means to shift the bits twice to the left:

    0010 -> 0100 -> 1000

    1000 is the binary representation for 8. Mathematically, left shifting is the same as multiplying a number by a power of 2 : a<<b == a*2^b , but as the operation is done only by shifting, it is much faster than doing multiplications.

    0 讨论(0)
  • 2021-01-18 14:01

    << it's the left-shift operator in Python. Take a look at the documentation for further details.

    0 讨论(0)
  • 2021-01-18 14:02

    This is left shift operator

    1<<16 implies 1 to be shifted left by 16 bits.

    0 讨论(0)
  • 2021-01-18 14:04

    Another way to think about it is 1 times 2^16.

    So whenever you see x << y interpret it as:

    x * 2^y

    0 讨论(0)
提交回复
热议问题