为什么表达式(0 == 0& 1 == 1)的计算结果为False? [英] Why is the expression (0==0 & 1==1) evaluating to False?

查看:184
本文介绍了为什么表达式(0 == 0& 1 == 1)的计算结果为False?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似地(-1 ==-1& 1 == 1)也为False.

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

很抱歉,这很明显,但是我找不到解释.

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

推荐答案

&按位AND 运算符.如文档所述,按位运算符的优先级高于逻辑运算符,所以

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

0 == 0 & 1 == 1

成为

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

假设您想要的是逻辑与,则实现此目标的python方法是使用and:

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

0 == 0 and 1 == 1

如您所愿,它将给您True.

这篇关于为什么表达式(0 == 0& 1 == 1)的计算结果为False?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆