Question from challenge | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Question from challenge

Why answer of this code is 2? Code: if 1&2: print(1) else: print(2)

5th Apr 2021, 4:07 AM
Sanjyot21
Sanjyot21 - avatar
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.
5th Apr 2021, 4:29 AM
Ipang
+ 2
Ipang Thanks for the explanation
5th Apr 2021, 4:42 AM
Sanjyot21
Sanjyot21 - avatar
0
No problem šŸ‘Œ
5th Apr 2021, 4:43 AM
Ipang