问题
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}
So, why do we need 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.
For numerical comparisons in C, in the case where short circuit is not needed, it seems that logical and bitwise have identical behaviour. In my case, bitwise ops are faster than logical.
I apologize for not putting these details in the original posting.
回答1:
I think no, take this example (0b - means binary):
a = 0b00000010
b = 0b00000100
Now, neither a
nor b
is 0. But a & b == 0
(due to the way bitwise AND is defined).
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).
Also there is short circuit evaluation, if in &&
left operand is 0, the right one will not be evaluated because result is certainly 0 (e.g., as already mentioned 0 && x == 0
no matter value of x
).
回答2:
- The logical operators convert their operands to
bool
before combining them and the result of a logical operator is alsobool
all the time. 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).
- 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.
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
}
回答3:
Logical operators are to compare multiple values true-itiveness.
For example, you use the &&
operator to see if two values are both 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:
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.
Both types of operators are similar in that they both either yield 0 or 1.
For example, take these:
1 || 0
The above would yield 1
because either of the values is equal to 1
. However, if we switch the operator to &&
, it would yield 0.
Out of all the operators you try, they will all either give 1
or 0
, true
or false
. 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; a bit is either 1 or 0.
回答4:
Not all expressions used as booleans evaluate to either 0 or 1; while 0 is treated as false, anything other than 0 is treated as true. So, for example, 1 && 2
would be true, but 1 & 2
would not, even though both 1 and 2 would be considered true.
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).
来源:https://stackoverflow.com/questions/32300522/whats-the-point-of-logical-operators-vs-bitwise-operators