What does << do in Python?

I solved the problem in Project Euler, but it took about 4 minutes to start, which is higher than the recommended time, so I looked at various solutions on the forum. One of them included a symbol <<in the list comprehension. Here is how it looked

blist.extend([(i << 1) + 3 for i in range(num) if alist.get(i)])  

I can not find what exactly this symbol does <<. Can anybody help me?

+5
source share
3 answers

This is aa bit change operator (Python docs) and is common among many programming languages ​​such as C, Java, PHP, etc. According to Python docs:

They carry the first argument on the left or right by the number of bits specified by the second argument.

n pow (2, n). n pow (2, n). ValueError.

, i << 1 1 , 2 ^ 1 2.

+10

.

x << n
x shifted left by n bits

x >> n
x shifted right by n bits
+1

This is the left shift operator. It shifts all the bits in i to the left by 1 step, effectively multiplying i by 2.

http://docs.python.org/py3k/reference/expressions.html#shifting-operations

+1
source

All Articles