c++ - What's the point of logical operators vs. bitwise operators -
given statement logical operation
((a > 5) && (b > 4))
and statement bitwise-operation
((a > 5) & (b > 4))
above 2 statements not equivalent.
because (a > 5)
element of {0,1}
so, why need logical operators & bitwise-operation
?
edit: of feedback. regarding short circuit behaviour of logical operators, not want behaviour - writing code gpu branches degrade performance: short circuit results in 2 branches instead of 1 in code.
for numerical comparisons in c, in case short circuit not needed, seems logical , bitwise have identical behaviour. in case, bitwise ops faster logical.
i apologize not putting these details in original posting.
i think no, take example (0b - means binary):
a = 0b00000010 b = 0b00000100
now, neither a
nor b
0. a & b == 0
(due way bitwise , defined).
however a && b != 0
(because logical , result 0 if @ least 1 operand 0 - not case a
, b
above).
also there short circuit evaluation, if in &&
left operand 0, right 1 not evaluated because result 0 (e.g., mentioned 0 && x == 0
no matter value of x
).
Comments
Post a Comment