0

What does << mean in Python??

X=5 print(x<<2) Output = 20

26th Sep 2019, 7:23 PM
Troy Kelley
Troy Kelley - avatar
9 Réponses
26th Sep 2019, 7:27 PM
Faisal
Faisal - avatar
+ 4
x << y Moves all 1s in x's bit format to left by y steps. 50 << 3 50 equals 110010 in binary. = 000110010 << 3 = 001100100 << 2 = 011001000 << 1 = 110010000 110010000 = 400 That's why 50 << 3 would equal 400
26th Sep 2019, 8:44 PM
Seb TheS
Seb TheS - avatar
+ 3
So in other words, 50 * (2*2*2) for 3 bits?
27th Sep 2019, 1:56 AM
Troy Kelley
Troy Kelley - avatar
+ 2
Troy Yes, bit operators can also be faster than basic operators. There is also >> which differs by moving bits y steps right instead. 50 >> 3 = 6
27th Sep 2019, 4:26 AM
Seb TheS
Seb TheS - avatar
+ 2
np
31st Jul 2020, 8:50 PM
Seb TheS
Seb TheS - avatar
0
This is the same as multiplying x by 2**2 (shifting of 2)
26th Sep 2019, 7:34 PM
Danny Eeraerts
Danny Eeraerts - avatar
0
Thank you!
27th Sep 2019, 2:04 AM
Troy Kelley
Troy Kelley - avatar
0
Thank you Seb!
28th Sep 2019, 12:10 AM
Troy Kelley
Troy Kelley - avatar
0
Thank you!!
31st Jul 2020, 8:48 PM
Troy Kelley
Troy Kelley - avatar