Explanation:
1. Assigning x
x = 1 << 4
<< is the left shift operator.
It shifts the binary value of 1 four positions to the left.
1 = 00001
After shifting:
00001 << 4
= 10000
Binary 10000 is 16 in decimal.
So:
x = 16
2. Calculating x - 1
x - 1
Since:
x = 16
we get:
16 - 1 = 15
In binary:
16 = 10000
15 = 01111
3. Bitwise AND: x & (x - 1)
Now the expression becomes:
16 & 15
Binary representation:
10000
& 01111
-------
00000
The & operator gives 1 only when both corresponding bits are 1.
Here, there is no position where both bits are 1.
Therefore:
16 & 15 = 0
4. print() Executes
print(x & (x - 1))
The calculated result is:
0
✅ Final Output
0

0 Comments:
Post a Comment