Why use logical operators when bitwise operators do the same?

Consider this condition:

(true & true & false & false & true) == true //returns: false 

As you can see, the bitwise behavior of the IO exactly matches the logical AND:

 (true && true && false && false && true) == true //returns: false 

I wonder why I should use logical operations when bitwise operations perform the same actions as logical ones.

Note. Please do not respond to this because of a performance issue, because it is much faster in Mozilla Firefox, see this jsPerf: http://jsperf.com/bitwise-logical-and

+7
source share
10 answers

The most common use of short-circuit evaluations using logical operators is not performance, but error elimination. See this:

 if (a && a.length) 

You cannot just use & here.

Note that using & instead of && not possible if you are not dealing with booleans. For example, & at 2 ( 01 in binary format) and 4 ( 10 in binary format) 0 .

Note also that, in addition to if tests, && (like || ) is also used because it returns one of the operands :

 "a" & "b" => 0 "a" && "b" => "b" 

More generally, using & instead of && often possible. Just like skipping a majority ; in your javascript code. But it will make you think more than you need (or it will sometimes cause strange errors).

+20
source

Is the bitwise behavior of operations the same?

No no. Bitwise operators work with integers, while logical operators have different semantics. Only when using pure Boolean results can the result be similar.

  • Bitwise operators : Evalutate and operands, convert to a 32-bit integer, work with them and return a number.
  • Logical operators : Evaluate the first operand, if it is true / false, then evalutate and return the second operand else, return the first result. This is called Short Circuit Rating.

You already see this difference in the type of result:

 (true & true & false & false & true) === 0 (true && true && false && false && true) === false 
+11
source

No, they do not do the same. The differences are as follows:

  • Are operand types converted
  • Will both operands be evaluated
  • Return value
 // sample functions function a() { console.log("a()"); return false; } function b() { console.log("b()"); return true; } 

&& (Logical AND)

  • Checks the veracity of operands
  • Uses a short circuit and may not evaluate the second operand
  • Returns the last processed operand without type conversion
 a() && b(); // LOG: "a()" // RET: false 

& (Bitwise AND)

  • Temporarily converts operands to their 32-bit integer representation (if necessary)
  • Computes both operands
  • Returns a number
 a() & b(); // LOG: "a()" // LOG: "b()" // RET: 0 
+8
source

Because using && or & conveys different intentions.

The first one says that you are testing validity .

The second means that you invoke magic. In real code, you will look at variable1 & variable2 . It will not be clear that you actually intend to verify the truth ( not truthfulness). The code reader is likely to be confused because it is not clear why & used.

In addition, the semantics are completely different, given other values ​​than bools and function calls, as evidenced by numerous other messages.

+3
source
  • Boolean is short-circuited, which can be an increase in performance or safety control.
  • Non-zero values ​​used in the conditional expression. For example, if ( 1 & 2 ) will return false, while if ( 1 && 2 ) will return true.
+2
source

Almost everything has already been said, but for completeness, I want to take a look at the performance aspect:

JavaScript has many hard-to-remember rules on how to evaluate expressions. This includes many castings when it comes to more complex comparisons. Arrays and objects must be converted by calling their toString() methods, and then they are thrown into numbers. This is a huge success.

Consider the following short circuit example when an array and an object are involved:

 ( false & {} & [] ) == true ( false && {} && [] ) == true 

Performance matters

+2
source

You cannot shorten bitwise operators. Bitwise operators can also do much more than just evaluate a logical expression.

+1
source

There is a huge difference: logical operations are short-circuited. This means that (true && true && false) is the last thing to do. This allows you to use powerful constructs, such as the abstract factory model, using var myFunc = mozilla.func || opera.sameFunc || webkit.evenOneMoreVariationOfTheSameConcept; var myFunc = mozilla.func || opera.sameFunc || webkit.evenOneMoreVariationOfTheSameConcept;

All bitwise subexpressions must be fully evaluated - and btw. in any case, it is rarely necessary to evaluate constant bitwise or logical expressions.

+1
source

The first condition is to convert the first and the sum of the bits then. But the second will check the boolean and return value.

So, the first will be slower than the second.

Run this test: http://jsperf.com/bitwise-logical

In Chrome and IE Bitrate is slower, but in FireFox the logical one is slower

+1
source
 var bit1 = (((true & true) & (false & false)) & true), // return 0; bit2 = (((true && true) && (false && false)) && true); // return flase alert(bit1 + ' is not ' + bit2); 
-one
source

All Articles