I thought it would be equal to 1<<7 == 128, but why is it equal to 1<<8 == 256? Could u please explain the algebra of x<<y + x<<z operations?
Asked
Active
Viewed 46 times
-2
-
1`(1 << (3 + 1)) << 4 == (1 << 4) << 4 == 16 << 4 == 256` – khelwood Sep 23 '20 at 13:18
-
3[python.org operator precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence) – khelwood Sep 23 '20 at 13:22
1 Answers
1
This is because of the python order of operations, where addition is evaluated before the left shift operator. The expression is equivalent to
(1<<(3+1))<<4

Rishi Verma
- 51
- 3