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

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

问题描述

同样的 (-1==-1 & 1==1) 也是假的.

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

假设您想要的是逻辑 AND,那么 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.

Which gives you True as you'd expect.

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

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