+ 2
What does << mean?
I don’t remember seeing this function in the lessons, but it keeps popping up in the challenge questions. I’ve tried to reverse engineer from the answers, but it still doesn’t make any sense to me. Can someone pls help? E.g. what would the output of: print(5>>2)
2 odpowiedzi
+ 7
as Quentin said, I ll just add
left shift means multiply by 2,
11<<1 is 110 ( 3 becomes 6)
11 << 2 is 1100 ( 3 becomes 12)
so
x << num , means x *2 ^num
conversely , >> right shift means divide by 2, so
5 >> 1 is 5/2 = 2
5 >> 2 is 5 / 2 = 2 then again 2/ 2 = 1
so 1 is the answer
+ 7
"5>>2" push all the bits of "5" written in binary (so "101"). It pushes it 2 places to the right, so it becomes 1. If you did "5<<3" instead it would have push it 3 places to the left adding zeros at its right and it would have returned "10100"