Explanation:
Code
print(True << 3 | False)
Heading: Step 1 — True as an Integer
In Python, Boolean values behave like integers in arithmetic and bitwise operations:
True = 1
False = 0
So the expression becomes:
1 << 3 | 0
Heading: Step 2 — Left Shift <<
1 << 3
The << operator shifts the binary bits 3 positions to the left.
Binary representation:
1 → 0001
After shifting 3 positions:
0001 << 3
1000
Binary 1000 is decimal 8.
Therefore:
1 << 3
gives:
8
Heading: Step 3 — Bitwise OR |
Now we have:
8 | 0
Binary:
8 → 1000
0 → 0000
Bitwise OR gives 1 whenever at least one corresponding bit is 1:
1000
0000
----
1000
1000 in binary is 8.
Heading: Step 4 — Final Output
Therefore:
print(True << 3 | False)
produces:
8
Final Answer
Output: 8

0 Comments:
Post a Comment