布尔运算符与位运算符

16 浏览
0 Comments

布尔运算符与位运算符

我对何时使用布尔运算符和按位运算符感到困惑:

  • and vs &
  • or vs |

有人可以告诉我何时使用每个运算符,以及使用其中一个会如何影响我的结果吗?

0
0 Comments

布尔运算符和位运算符是编程中常用的两种操作符。尽管它们在某些情况下可以产生相同的结果,但它们在操作方式和应用场景上存在一些区别。

首先,布尔运算符andor是从布尔逻辑中直接引入的(因此操作两个布尔值,产生一个布尔值),而位运算符&|则是将布尔与/或运算应用到整数的各个位上。这就导致了许多关于位运算符如何工作的问题。

下面是可能影响结果的一些实际区别:

1. andor具有短路的特性,例如True or sys.exit(1)不会退出,因为对于第一个操作数的某个值(True or ...False and ...),第二个操作数不会改变结果,所以不需要进行评估。但|&不具有短路特性 - True | sys.exit(1)将使你退出REPL(交互式解释器)。

2. &|是常规的运算符,可以进行重载,而andor是语言中固定的(尽管用于强制转换为布尔值的特殊方法可能会有副作用)。

3. andor返回操作数的值,而不是TrueFalse。这不会改变条件中布尔表达式的含义 - 1 or True的结果是1,但1也是True。但曾经使用它们来模拟条件运算符(C语法中的cond ? true_val : false_val,Python中的true_val if cond else false_val)。对于&|,结果类型取决于操作数如何重载相应的特殊方法(True & False的结果是False99 & 7的结果是3,对于集合来说,是并集/交集...)。

然而,即使在某些情况下a_boolean & another_boolean可以产生相同的结果,正确的解决方案仍然是使用and - 因为andor与布尔表达式和条件相关,而&|表示位操作。

0
0 Comments

布尔运算符通常用于布尔值,而位运算符通常用于整数值。

布尔运算符是短路的,但位运算符不是短路的。

短路行为在以下表达式中非常有用:

if x is not None and x.foo == 42:
    # ...

如果使用位运算符`&`,这将无法正常工作,因为两边的表达式总是会被求值,从而导致`AttributeError: 'NoneType' object has no attribute 'foo'`。当使用布尔运算符`and`时,当第一个表达式为False时,不会对第二个表达式进行求值。类似地,当第一个表达式为True时,`or`不会对第二个参数进行求值。

补充:在Python中,`&`、`|`、`^`也是集合操作。

额外补充:在Python中,位运算不允许混合类型,但布尔运算允许。例如,`True or "True"`是可以的(它将返回第一个真值),但`True | "True"`会抛出异常。这与混合类型无关。位运算只对整数有意义,对于任何其他类型都会抛出异常。这里的重点是Python将False和True分别视为0和1:`0 == False` 和 `1 == True` 都为真。

原因:布尔运算符和位运算符在使用时有一些不同的行为,这可能导致程序运行时出现错误。

解决方法:根据具体的需求选择正确的运算符来使用,如果需要短路行为,则使用布尔运算符,如果需要对整数进行位操作,则使用位运算符。

文章如上所述,总结了布尔运算符和位运算符之间的区别以及使用时可能遇到的问题和解决方法。

0
0 Comments

Boolean operators and bitwise operators are two different types of operators in programming languages. The issue with their precedence led to confusion in the evaluation of expressions.

The problem arises because bitwise operators, such as "&", have a higher precedence than boolean operators, such as "and". This means that when both types of operators are used in an expression, the bitwise operator is evaluated first.

In the given example, the expression "0 < 1 & 0 < 2" is evaluated differently than "0 < 1 and 0 < 2".

In the first expression, the bitwise operator "&" is evaluated first, resulting in "1 & 0" which is equal to 0. So the expression becomes "0 < 0 < 2", which is equivalent to "False".

In the second expression, the boolean operator "and" is evaluated first. So the expression becomes "0 < 1" which is True, and then "True and 0 < 2" which is also True.

To solve this issue and ensure the desired evaluation of expressions, parentheses can be used to explicitly define the order of evaluation. By using parentheses, the expressions can be written as:

(0 < 1) & (0 < 2)

and

(0 < 1) and (0 < 2)

By adding parentheses, the precedence of the operators is explicitly defined, and the expressions will be evaluated correctly.

In conclusion, the issue of different evaluation results in expressions involving boolean operators and bitwise operators can be resolved by using parentheses to explicitly define the order of evaluation.

0