And
From Xojo Documentation
Used to perform a logical comparison of two boolean expressions and a bitwise And comparison of two integer expressions.
Usage
result = expression1 And expression2
Part | Description |
---|---|
result | A Boolean if expression1 and expression2 are booleans; an integer if expression1 and expression2 are integers. |
expression1 | Any valid boolean or integer expression. The data types of expression1 and expression2 must match. |
expression2 | Any valid boolean or integer expression. The data types of expression1 and expression2 must match. |
Notes
The And operator is overloaded. You can pass either two booleans or two integers. If you pass two booleans, And performs a logical And operation and returns a boolean. If you pass two integers, it performs a bitwise And operation and returns an integer. In this case, it is equivalent to calling the BitAnd method of the Bitwise class.
In the first instance, if both boolean expressions evaluate to True, then result is True. If either expression evaluates to False then result is False.
The truth table for logical operators is shown below.
Expression1 | Expression2 | And | Or | Xor |
---|---|---|---|---|
True | True | True | True | False |
True | False | False | True | True |
False | True | False | True | True |
False | False | False | False | False |
If you pass two integers, And returns an integer that is the result of comparing each bit of the two integers and assigning 1 to the bit position in the integer returned if both bits in the same position in the integers passed are 1. Otherwise, 0 is assigned to the bit position.
The following table gives the results for bitwise operators.
Integer1 | Integer2 | And | Or | Xor |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
The Operator_And function can be used to define the And operator for classes.
Use within If Statements
When used within an If..Then statement, once the first expression results to False, none of the subsequent expressions are evaluated. In this example, the IsValid method is not called because the first expression is False. This is referred to as short-circuit evaluation:
Sample Code
This example uses the And operator to perform logical comparisons.
Dim b As Boolean
Dim c As Boolean // defaults to False
Dim d As Boolean // defaults to False
a = True
b = True
d = a And b // Returns True
d = a And c // Returns False
Using And with Integer values:
To understand how the above evaluates to 4, look at the binary values. The value of 4 is "100" in binary. The value of 5 is "101" binary. If you "And" those two binary values together then only the first "1" is in common. So the result is "100", which is 4.
See Also
Not, Or, Xor operators; Bitwise class; Operator_And function; Operator precedence