Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the expression (0==0 & 1==1) evaluating to False?

Similarly (-1==-1 & 1==1) is also False.

Apologies if this is something obvious but I can't find an explanation for it.

like image 588
PollPenn Avatar asked Oct 17 '17 05:10

PollPenn


People also ask

What is the value of 0 < 0 == 0?

So your expression 0 < 0 == 0 is really (0 < 0) and (0 == 0), which evaluates to False and True which is just False. Show activity on this post.

Why Log (0) is not defined?

Why log (0) is not defined. The real logarithmic function log b (x) is defined only for x>0. We can't find a number x, so the base b raised to the power of x is equal to zero: b x = 0 , x does not exist So the base b logarithm of zero is not defined. For example the base 10 logarithm of 0 is not defined:

When does a function become continuous at x = 0?

If 0 0 = 0, \frac00=0, 0 0 ​ = 0, the function f ( x) = 0 x f (x)=\frac0x f ( x) = x 0 ​ becomes continuous at x = 0. x=0. x = 0. But this is not satisfactory in all cases, and the arbitrariness of the choice will break other laws of arithmetic. For instance,

Why does pythons evaluate to false when 0 is 0?

The strange behavior your experiencing comes from pythons ability to chain conditions. Since it finds 0 is not less than 0, it decides the entire expression evaluates to false. As soon as you break this apart into seperate conditions, you're changing the functionality.


2 Answers

& is the bitwise AND operator. As mentioned in the documentation, Bitwise operators have higher precedence than logical operators, so

0 == 0 & 1 == 1

Becomes

0 == (0 & 1) == 1

And you can imagine it goes downhill from there:

   0 == (0 & 1) == 1
=> 0 == 0 == 1
=> 0 == 0 and 0 == 1
=> True and False 
=> False

Assuming what you wanted was a logical AND, the python way to do that would be using and:

0 == 0 and 1 == 1

Which gives you True as you'd expect.

like image 148
cs95 Avatar answered Oct 29 '22 01:10

cs95


Lets break this up.

The highest priority sign here is the brackets. Except we're wrapping the entire expression, so they don't do anything.

Next we have the bitwise operator &.

0 & 1 which equals 0.

This leaves us with 0 == 0 == 1

As 0 does not equal 1, we get False.

For reference, here is the python documentation about operator precedence.

like image 42
Shadow Avatar answered Oct 29 '22 01:10

Shadow