The “&” and “&&” both are the operators, used to evaluate the conditional statements. The & operator is a logical as well as, a bitwise operator. The && operator is purely a Logical operator. The basic difference between the & and && operator is that the & operator evaluate both sides of the expression whereas, the && operator evaluates only the left-hand side of the expression to obtain the final result.
Let us understand the other differences between & and && with the help of comparison chart.
Content: & Vs &&
|Basis for Comparison||&||&&|
|Operator||It is a "Bitwise Operator".||It is a "Logical Operator".|
|Evaluation||It evaluates both left and right side of the expression.||It only evaluates the left side of the expression.|
|Operates on||It operates on "Boolean datatype" as well as operates on "bits".||It operates only on "Boolean datatype".|
|Use||Use to check logical condition and also used to mask off certain bits such as parity bits.||Used only to check logical condition.|
Definition of & ( Bitwise AND)
This ‘&’ operator is used as both, a logical (&) operator and a bitwise operator. It works on boolean as well as binary data. When & operator is used as a logical & operator then, it results in “true” if both the side of the expression of evaluation are true, else it returns “false”.
It allows the compiler to evaluate both the side of the expression. That is, even if left-hand side of the expression results false, it evaluates the right side of the expression.
Lets us understand this with an example.
int a=4, b=5; system.out.println((a==6) &(b++==6)); system.out.println("b= "+b); //output false b=5
Here, on evaluating the left side of the expression (a==6), it results in false, the & operator then evaluate the right side of the expression (b++==6) as a result, the value of b increments.
When ‘&’ used as a “bitwise operator”, it first converts both the operands into binary form and then operates on it using & operator, bit-by-bit. After the operation, the result obtained, is in binary form, which is then converted into decimal.
Any bit that is 0 in either of the operands results in 0. If both the bits of the operands is 1, then the resultant bit is 1. Bitwise & operator is governed by the same truth table as by its logical & operator.
Let us see the bitwise operation of & operator.
int a; a = 3 & 4; // 011 & 100 = 000 system.out.println("a= "+a); //output a= 0
Here, the decimal value 3 and 4 are initially converted into their binary form, and then the & bitwise operator perform the & operation on them bit-by-bit. The result obtained is in binary form, which is then converted to decimal form again.
Definition of && (Short-circuit AND)
This && operator work totally as a logical operator. It operates only on the boolean data type. It is also called short-circuit operator. As it only checks the left-hand side of the expression. If the left-hand side of the expression results in false, then it does not bother about evaluating the right-hand side of the expression.
Lets us understand the working of the && operator with an example.
int a=4, b=5; system.out.println((a==6) && (b++==6)); system.out.println("b= "+b); //output false b=4
Here, the as the condition (a==6) is false, the && operator does not evaluate the expression(b++==6) as a result, the value of b doesn’t increments.
Key Differences Between & and &&
- The & operator is a logical as well as a bitwise operator, as it operates on both booleans as well as binary data whereas, the && operator is only a logical operator as it operates only on a boolean data type.
- The & operator evaluates both the side of the expression to obtain the final result whereas, the && operator evaluates only the left side of the expression & if turn out false it doesn’t even evaluate the right side of the expression.
The & and && operator, both are used to evaluate the boolean condition whereas, the & operator is also used for the bitwise operation. When we require to evaluate both the side of the expression the & operator is used else we can use && operator.