And Operator
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

And Operator

Performs a logical conjunction on two Boolean expressions, or bitwise conjunction on two numeric expressions.

result = expression1 And expression2


Required. Any Boolean or numeric expression. The result for Boolean comparison is the Boolean result of comparison of the two expressions. The result for numeric comparison is a numeric value resulting from the bitwise conjunction of two numeric expressions
Required. Any Boolean or numeric expression.
Required. Any Boolean or numeric expression.


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 ensure 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 > B And B > C   ' Returns True.
myCheck = B > A And 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.

See Also

Logical/Bitwise Operators | Operator Precedence in Visual Basic | Operators Listed by Functionality | AndAlso Operator | Logical Operators

© 2016 Microsoft