简体   繁体   English

逻辑运算符与按位运算符的区别是什么

[英]What's the point of logical operators vs. bitwise operators

Given this statement is a logical operation 鉴于此声明是合乎逻辑的操作

 ((a > 5) && (b > 4))

And this statement is bitwise-operation 这句话是按位运算

   ((a > 5) & (b > 4)) 

Above two statements is not equivalent. 以上两个陈述并不等同。

Because (a > 5) is an element of {0,1} 因为(a > 5){0,1}的元素

So, why do we need logical operators & bitwise-operation ? 那么,为什么我们需要logical operators & bitwise-operation呢?

Edit : thanks for all of the feedback. 编辑 :感谢您的所有反馈。 Regarding the short circuit behaviour of logical operators, I actually do not want this behaviour - I am writing code for a GPU where branches degrade performance: short circuit results in two branches instead of one in the code. 关于逻辑运算符的短路行为,我实际上不希望这种行为-我正在为GPU编写代码,其中分支降低了性能:短路导致两个分支而不是代码中的一个。

For numerical comparisons in C, in the case where short circuit is not needed, it seems that logical and bitwise have identical behaviour. 对于用C进行的数值比较,在不需要短路的情况下,似乎逻辑和按位具有相同的行为。 In my case, bitwise ops are faster than logical. 就我而言,按位运算比逻辑运算快。

I apologize for not putting these details in the original posting. 对于没有在原始帖子中添加这些详细信息,我深表歉意。

I think no, take this example (0b - means binary): 我想不,以这个例子为例(0b-表示二进制):

a = 0b00000010
b = 0b00000100

Now, neither a nor b is 0. But a & b == 0 (due to the way bitwise AND is defined). 现在, ab都不为0。但是a & b == 0 (由于按位与的定义方式)。

However a && b != 0 (because for logical AND result is 0 if at least one operand is 0 - this is not the case with a and b above). 但是a && b != 0 (因为如果至少一个操作数为0,则逻辑AND结果为0-上面的ab则不是这种情况)。


Also there is short circuit evaluation, if in && left operand is 0, the right one will not be evaluated because result is certainly 0 (eg, as already mentioned 0 && x == 0 no matter value of x ). 也有短路的评价,如果&&左侧的操作数是0,正确的将不进行评估,因为结果当然是0(例如,前面已经提到0 && x == 0的不管值x )。

  • The logical operators convert their operands to bool before combining them and the result of a logical operator is also bool all the time. 逻辑运算符在组合它们之前将其操作数转换为bool ,逻辑运算符的结果始终都是bool The bitwise operators do not do this (however in case of operands that are bool anyway this doesn't make a difference between the two kind of operators). 按位运算符不执行此操作(但是无论如何,对于布尔运算符,这两种操作符之间没有区别)。
  • Logical operators can be used on a lot of operand types that can be converted to bool while bitwise operators work only on a few types in specific cases and the output of bitwise operators are typed (not always bool). 逻辑运算符可用于许多可转换为bool的操作数类型,而按位运算符在特定情况下仅可用于少数类型,并且按位运算符的输出是类型化的(并非始终为bool)。
  • the logical operators are shortcut. 逻辑运算符是捷径。 For example in case of && this means: if the first operand is false, then the second operand isn't even evaluated because the whole expression (false && something) is already false regardless of the value of the second operand. 例如,对于&&这意味着:如果第一个操作数为false,则第二个操作数甚至都不会求值,因为无论第二个操作数的值如何,整个表达式(false && something)已经为false。

The logical shortcut operators are often exploited in cases like the following: 逻辑快捷方式运算符通常在以下情况下被利用:

// If mypointer is NULL, then mypointer->is_whatever()
// isn't evaluated so it doesn't cause a NULL pointer crash.
if (mypointer && mypointer->is_whatever()) {
    // do my thing
}

Logical operators are to compare multiple values true-itiveness. 逻辑运算符将比较多个值的真实性。

For example, you use the && operator to see if two values are both true . 例如,使用&&运算符查看两个值是否均为true


Bit-wise operators are for isolating and modifying bits in a value. 按位运算符用于隔离和修改值中的位。

For example, in order to turn off all the bits in an 8-bit value except for one, you would do this: 例如,要关闭一个8位值中的所有位(除了一位),您可以这样做:

val & 00100000

In the above example, the 6th (1-based) or 5th (0-based) bit is kept as it was, and the other bits are turned off. 在上面的示例中,第6位(从1开始)或第5位(从0开始)保持原样,其他位关闭。


Both types of operators are similar in that they both either yield 0 or 1. 两种类型的运算符都相似,它们都产生0或1。

For example, take these: 例如,采取以下措施:

1 || 0

The above would yield 1 because either of the values is equal to 1 . 上述将产生1 ,因为任一值等于1 However, if we switch the operator to && , it would yield 0. 但是,如果将运算符切换为&& ,它将产生0。

Out of all the operators you try, they will all either give 1 or 0 , true or false . 在您尝试的所有运算符中,它们都将给出10truefalse That is because there is no between: there is no such thing as an expression evaluating to "sort-of true" or "maybe false". 那是因为两者之间没有关系:不存在诸如表达式的评估结果为“真”或“假”之类的东西。

And I think the reason why a bit-wise operator always "yields" 1 or 0 is pretty self explanatory: bit -wise; 我认为之所以逐位操作总是“产生” 1或0是非常自我解释: -wise; a bit is either 1 or 0. 位是1或0。

Not all expressions used as booleans evaluate to either 0 or 1; 并非所有用作布尔值的表达式的取值都为0或1; while 0 is treated as false, anything other than 0 is treated as true. 当0被视为false时,除0以外的任何值都被视为true。 So, for example, 1 && 2 would be true, but 1 & 2 would not, even though both 1 and 2 would be considered true. 因此,例如,即使1和2都被认为是真实的, 1 && 2才是真实的,而1 & 2不是。

Also, as others have pointed out, the logical operators will not evaluate the second expression if the first is enough to determine the overall value ('short-circuiting'), which obviously cannot be done with the bitwise version (well, not as often; 0 & ? will be 0 no matter what ? is, and thus ? wouldn't need to be evaluated to get the final value, but & doesn't work that way). 另外,正如其他人指出的那样,如果第一个表达式足以确定总值(“短路”),则逻辑运算符将不会对第二个表达式求值,这显然不能用按位形式完成(嗯,不是那么频繁) ; 0 & ?将是0,无论?是什么,因此不需要对?进行求值即可得出最终值,但&不能那样工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM