Jacko
Jacko

Reputation: 13195

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}

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.

Upvotes: 1

Views: 5014

Answers (3)

Giorgi Moniava
Giorgi Moniava

Reputation: 28654

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).

Upvotes: 7

Scott Hunter
Scott Hunter

Reputation: 49803

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).

Upvotes: 2

SirPython
SirPython

Reputation: 647

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.

Upvotes: 1

Related Questions