The logical AND operator is '&&'The result of the logical AND operator is 1 if both arguments are logical TRUE and it is 0 if either of its arguments are logical FALSE
logical TRUE is defined as any non-zero value
logical FALSE is defined as the value zeroPut this another way, the result of the logical AND operator is 1 if both arguments are not 0 and it is 0 if either of its arguments are 0
If we call the inputs A and B and the output C we can show the logical AND function as:
The binary AND function operates on all bits in parallel (see binary AND for more details)
A B C FALSE && FALSE -> FALSE FALSE && TRUE -> FALSE TRUE && FALSE -> FALSE TRUE && TRUE -> TRUE Logical AND and binary AND differ subtlely.
A B C 0 & 0 -> 0 0 & 1 -> 0 1 & 0 -> 0 1 & 1 -> 1 With locical AND, 0x02 is logical TRUE and 0x04 is also logical TRUE
so (0x02 && 0x04) results in 0x01 which is logical TRUE
whereas (0x02 & 0x04) results in 0x00 which is logical FALSE
binary logical logical A B C A B C A B C 0 & 0 -> 0 0 && 0 -> 0 FALSE && FALSE Logical AND Operator-> FALSE 0 & 1 -> 0 0 && 1 -> 0 FALSE && TRUE -> FALSE 1 & 0 -> 0 1 && 0 -> 0 TRUE && FALSE -> FALSE 1 & 1 -> 1 1 && 1 -> 1 TRUE && TRUE -> TRUE
binary logical logical A B C A B C A B C 0x04 & 0x04 -> 0x04 0x04 && 0x04 -> 0x01 TRUE && TRUE -> TRUE 0x04 & 0x02 -> 0x00 0x04 && 0x02 -> 0x01 TRUE && TRUE -> TRUE 0x02 & 0x04 -> 0x00 0x02 && 0x04 -> 0x01 TRUE && TRUE -> TRUE 0x02 & 0x02 -> 0x02 0x02 && 0x02 -> 0x01 TRUE && TRUE -> TRUE
BEWARE:
Notice how in the above case binary AND results in 2 TRUE and 2 FALSE results whereas the logical AND for the same inputs results in 4 TRUE results.
binary logical A B A B A B 0x04 & 0x04 -> 0x04 TRUE 0x04 && 0x04 -> 0x01 TRUE 0x04 & 0x02 -> 0x00 FALSE 0x04 && 0x02 -> 0x01 TRUE 0x02 & 0x04 -> 0x00 FALSE 0x02 && 0x04 -> 0x01 TRUE 0x02 & 0x02 -> 0x02 TRUE 0x02 && 0x02 -> 0x01 TRUE
See Binary AND for further clarification
The logical AND operator is an early out operator. This means that as soon as the result is known, the remainder of the expression is ignored.How is this possible? It is possible because if the first argument evaluates to FALSE then no mater what value the second argument has the result will always be FALSE
NOTE: the binary AND operator is NOT an early out operator
Why is this important?
The early out property of the logical AND operator makes it useful as a gate or guard mechanism in expressions.
Consider the statement
if arr[j] == 0 thenThis could cause a problem is j is greater than the maximum number of elements in the array arr. changing this statement to
if j < 10 && arr[j] == 0 thencauses the array arr not to be touched if j is greater than or equal to 10
The logical AND is normally used in conditional expressions such as if and while statementse.g.
if X != 2 then // execute these statements if X is NOT equal to 2 if X != 3 then // execute these statements if X is NOT equal to 3 if X != 5 then // execute these statements if X is NOT equal to 5 j = j + X endif endif endifis equivalent toif X != 2 && X != 3 && X != 5 then // execute these statements if // X is NOT equal to 2 AND // X is NOT equal to 3 AND // X is NOT equal to 5 j = j + X endifThe use of logical AND makes the second piece of code easier to understand and more efficient.