本文介绍了为什么不是位运算符一样聪明的逻辑QUOT;和\\或QUOT;运营商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只注意到位运算并不像聪明的逻辑和\\或操作,我不知道为什么?

I just noticed that bitwise operations aren't as "smart" as logical "and\or" operations and I wonder why?

下面是一个例子:

// For the record
private bool getTrue(){return true;}
private bool getFalse(){return false;}

// Since a is true it wont enter getFalse.
bool a = getTrue() || getFalse(); 

// Since a is false it wont enter getTrue.
bool b = getFalse() && getTrue(); 

// Since b is false it wont enter getTrue.
b = b && getTrue();

然而,位运算符| =和&安培; =都不如智能:

However the bitwise operators "|=" and "&=" aren't as smart:

bool a = getTrue();
a |= getFalse(); // a has no chance to get false but it still enters the function.

a = getFalse();
a &= getTrue(); // a has no chance to get true but still performs this operation.

我不知道为什么他们没有在同一个逻辑的方式工作。

I wondered why they don't work in the same logical way.

推荐答案

澄清一点:

运营商&安培; = | = 评估时,不逐位运算符布尔秒 - 他们是逻辑运算符,但它们 X = X和放大器的同等学历;是 X = X |是,它不喜欢&放不短路;&安培; || 做的。

The operators &= and |= are NOT bitwise operators when evaluated on bools - they are logical operators, but they are the equivalent of x = x & y and x = x | y, which do not short circuit like && and || do.

&将功放;运营商按位进行逻辑与操作上的积分运算和逻辑和布尔运算。

设计师的可能的已实施 || = &放大器;&安培; = ,但由于他们的只有的是适当的布尔类型的,没有太多的价值在那里。

The designers could have implemented ||= and &&=, but since they would only be appropriate for boolean types, there's not much value there.

这篇关于为什么不是位运算符一样聪明的逻辑QUOT;和\\或QUOT;运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 20:06