0
Question from challenge
Why answer of this code is 2? Code: if 1&2: print(1) else: print(2)
3 Respostas
+ 4
& is the bitwise AND operator. It checks whether the binary representation of the LHS operand has the RHS operand bit set.
An example with 4 bits values
BINARY DECIMAL
0001 1 (LHS operand)
0010 2 (RHS operand)
----------------- &
0000 0 (result)
Bitwise AND operator checks and sets the bit in the (result) to 1 when the bit (at the same offset) taken from LHS and RHS operand were non zero.
Since the evaluation (result) yields 0 (a falsey value), the execution flow enters the `else` block rather than the `if` block.
Note:
LHS = Left Hand Side
RHS = Right Hand Side
LHS operand is the thing we see to the left of the operator, and RHS operand is the thing we see to the right of the operator.
+ 2
Ipang Thanks for the explanation
0
No problem 👌