strange question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

strange question

Can anyone explain me this? Ive looked into Google but found nothing x=5 print(x>>2) # result is 1 print(x<<2) # result is 20 print(x>>2<<x) # result is 32 i dont get it. What is >> or << ???

16th Jan 2017, 8:55 PM
guest013
guest013 - avatar
5 Answers
+ 8
>> shifts the bits of the binary version of 5 to the right, and << to the left. so x >> 2 shifts the bits 2 positions to the right.
16th Jan 2017, 9:14 PM
Karl T.
Karl T. - avatar
+ 5
you need to understand binary a bit.... decimal 5 is 00000101in binary 5>>2 is 00000001 which is 1 in decimal 5<<2 is 00010100 thats 16+4 =20 (5>>2)<<5 is 1<<5 , thats 00100000 which is 32 in decimal.
17th Jan 2017, 6:47 PM
ifl
ifl - avatar
+ 4
A >> n means "divide A by 2, n times" : A/(2^n) A << n means "multiply A by 2, n times": A* (2^n)
17th Jan 2017, 6:57 PM
ifl
ifl - avatar
+ 2
@ifl now its much clearer. Thanks for help
17th Jan 2017, 7:40 PM
guest013
guest013 - avatar
0
ummmmmmmmmm
17th Jan 2017, 10:52 AM
bob