Performs a logical conjunction on two Boolean expressions, or bitwise conjunction on two numeric expressions.
result = expression1 And expression2
For Boolean comparison, if both expression1 and expression2 evaluate to True, result is True. If expression1 evaluates to True and expression2 evaluates to False, result is False. If expression1 evaluates to False, and expression2 evaluates to True, the result is False. The following table illustrates how result is determined:
If expression1 is | And expression2 is | Value of result is |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
When applied to numeric values, the And operator performs a bitwise comparison of identically positioned bits in two numeric expressions and sets the corresponding bit in result according to the following table:
If bit in expression1 is | And bit in expression2 is | The result is |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Note Since the logical/bitwise operators have a lower precedence than other arithmetic and relational operators, any bitwise operations should be enclosed in parentheses to insure accurate execution.
If the operands consist of one Boolean expression and one numeric expression, the result Boolean expression will be converted to a numeric value (-1 for True, and 0 for False) and the bitwise operation will result.
This example uses the And operator to perform a logical conjunction on two expressions. The result is a Boolean value that represents whether the entire conjoined expression is true.
Dim A As Integer = 10 Dim B As Integer = 8 Dim C As Integer = 6 Dim myCheck As Boolean myCheck = A > BAnd
B > C ' Returns True. myCheck = B > AAnd
B > C ' Returns False.
This example uses the And operator to perform logical conjunction of the individual bits of two numeric expressions. The bit in the result pattern is set if the corresponding bits in the operands are both set.
Dim A As Integer = 10 Dim B As Integer = 8 Dim C As Integer = 6 Dim myCheck As Integer myCheck = (A And B) ' Returns 8. myCheck = (A And C) ' Returns 2. myCheck = (B And C) ' Returns 0.
Logical/Bitwise Operators | Operator Precedence in Visual Basic | Operators Listed by Functionality | AndAlso Operator |