+ 1

What does << do in Python?

20th Oct 2018, 10:41 AM
Gordon
Gordon - avatar
15 ответов
+ 4
What Taste said, only that 4 would be 0100, left shifted by 1 would be 1000 which is 8. A left shift by 1 is a multiplication by 2, a right shift by one is an integer division by 2 (1001 = 9, 1001 >> 1 = 0100 = 4). It can be used because it's very fast and bitwise operations allow for bit masking which can be used in micro processing, for encryption etc.
20th Oct 2018, 12:23 PM
Anna
Anna - avatar
+ 3
oh yeah right, i keep forget that binary start at 128, not 1 as my example above. thx Anna
20th Oct 2018, 12:33 PM
Taste
Taste - avatar
+ 3
Thanks Taste, Anna and Alex. Taste's try is nice and Sololearn cannot be a great place without people like you who likes to help others. Alex's question about why we should have this is very interesting. On one hand, we can learn more about the original purpose; On the other hand, we can try to innovate new purpose of using an operation. Also thank you Anna for the content rich reply. I wonder what would be the output if instead of a number I input two strings as self and other ,say Gordon << Alex But I'll try out in Code Playground first and if I don't understand, I'll come back for a answer.
20th Oct 2018, 1:22 PM
Gordon
Gordon - avatar
+ 2
its binary left-shift operator for example, if x = 4 x<<1 that means 4 which is 00001000 shift by 1 to left and become 00000100 and the result is 8. (edited)
20th Oct 2018, 10:52 AM
Taste
Taste - avatar
+ 2
there's some of case where you need to do that. i once use the operator to check color of a pixel, when work on translation black white image to binary for hidden message
20th Oct 2018, 11:55 AM
Taste
Taste - avatar
+ 2
yes, its binary. almost every digital data can be translated into binary.
20th Oct 2018, 12:04 PM
Taste
Taste - avatar
+ 2
Hi~ It's me again, with the result The result is: TypeError https://code.sololearn.com/cb08wrNak994/?ref=app
20th Oct 2018, 1:26 PM
Gordon
Gordon - avatar
+ 1
Why would someone want to do that?
20th Oct 2018, 11:51 AM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
+ 1
every color/number has his own sequence with 0 and 1? And if yes , where can i find a list or something with that?
20th Oct 2018, 12:03 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
+ 1
i'm not familiar with python myself, but maybe you can work on each character seperately using loop. then use ord() to get the int value of the character. it should be possible https://code.sololearn.com/cI6l7LCedFXW/?ref=app
20th Oct 2018, 1:48 PM
Taste
Taste - avatar
+ 1
Nice! ord() is to get ascii code?
20th Oct 2018, 2:23 PM
Gordon
Gordon - avatar
+ 1
What is the function to convert back, if I have the key "world"?
20th Oct 2018, 2:25 PM
Gordon
Gordon - avatar
+ 1
yes, it'll return the int for unicode code point its chr()
20th Oct 2018, 2:25 PM
Taste
Taste - avatar
20th Oct 2018, 2:44 PM
Gordon
Gordon - avatar
0
good to know :D
20th Oct 2018, 2:47 PM
Taste
Taste - avatar