0

How is (10|11) different from (10 or 11) ?

I know the first one that it does operation bitwise (Bitwise Operator), i.e. (10)=1010 or (11)=1011, individually sets bit value to 1 if both bits are 1, therefore, 10|11 is (1011) => 11, but don't know about the (10 or 11), all I know is it's a logical operator, but I don't how it operates here. Please help.

1st May 2020, 7:31 AM
Aanzil Akram Halsana
Aanzil Akram Halsana - avatar
2 Answers
+ 2
10 or 11 should give 10 since it's True ,greater than 0 but if it was 0 or 11 then answer would be 11
1st May 2020, 7:57 AM
Abhay
Abhay - avatar
+ 1
Aanzil Akram Halsana You're mistaking: 10 (bin 1010) | 11 (bin 1011) should give 11 (bin 1011)... 10 & 11 give you 10. bitwise OR return 1 for each bit set in either first or second operands... that's the bitwise AND wich "individually sets bit value to 1 if both bits are 1" ;P Anyway, difference with "or" logical operator is that logical operator first evaluate each operand (or only first one when enough to tell the answer) to boolean values (1 bit values: 0 or 1, commonly treated as True or False)... and everything wich is not zero (or empty object for built-in python objects/types, or again None) is True. And boolean (logical) expression doesn't return the boolean result but the first value wich make the expression evaluated: res = 42 or None give 42, as 42 is true, there's no need to evaluate second part of expression. res = 42 and None give None, as 42 is True, second part should be checked to know the result... res = 0 and None give 0 as 0 is False so result is False without going further ;)
1st May 2020, 9:38 AM
visph
visph - avatar