+ 1
What does << do in Python?
15 Réponses
+ 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.
+ 3
oh yeah right, i keep forget that binary start at 128, not 1 as my example above.
thx Anna
+ 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.
+ 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)
+ 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
+ 2
yes, its binary. almost every digital data can be translated into binary.
+ 2
Hi~ It's me again, with the result
The result is:
TypeError
https://code.sololearn.com/cb08wrNak994/?ref=app
+ 1
Why would someone want to do that?
+ 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?
+ 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
+ 1
Nice! ord() is to get ascii code?
+ 1
What is the function to convert back, if I have the key "world"?
+ 1
yes, it'll return the int for unicode code point
its chr()
+ 1
Taste It works!!!!!
https://code.sololearn.com/cb08wrNak994/?ref=app
0
good to know :D