0

How output is 32?

x = 5 print(x>>2<<x) =============== Output: 32

5th Aug 2021, 3:27 AM
Lelouch Vi Britannia
Lelouch Vi Britannia - avatar
2 Answers
+ 2
0. Know the operators. 1. Understand the order of operations. 2. Trace what happens in binary. Operators: >> causes rightward bit shift the given number of places << causes leftward bit shift the given number of places Order of operations: The order of operations for x>>2<<x is left-to-right because both operators have the same precedence. So it is equivalent to (x>>2)<<x. In binary: x = 5 # 5 is 101 in binary x>>2 # 101(bin)>>2 is 1 #note that the lowest 2 bits are lost (truncated) (1)<<x # 1<<5 is 100000(bin) # in decimal 2**5 is 32 Output: print(32)
5th Aug 2021, 3:12 PM
Brian
Brian - avatar
0
This is because of bitwise operator "What do >> and << mean in Python? - Stack Overflow" https://stackoverflow.com/questions/22832615/what-do-and-mean-in-python/22832675
5th Aug 2021, 4:01 AM
Pariket Thakur
Pariket Thakur - avatar