AND

Result = Expression AND Expression

Depending on the expression, the AND operation can be either a logical AND or a numerical AND. In case of two boolean expressions, a logical AND operation is performed. In case of two integer numbers, a numerical AND operation is performed.

Strings and objects are automatically converted to booleans.

A null string or a null object is converted to FALSE, other values are converted to TRUE.

Since 3.17

If one of the operands is a floating value, an error is raised.

Before Gambas 3.17, the floating point values were silently converted to Boolean, leading to a useless result.

The logical AND operator takes two boolean expressions and returns a true or false value. The results returned by this operator is shown in the following table:

A B A AND B
FALSE FALSE FALSE
FALSE TRUE FALSE
TRUE FALSE FALSE
TRUE TRUE TRUE

The numerical AND operator takes two integer values and returns an integer value. Each corresponding bit of the specified values are combined according to the following table:

A B A AND B
0 0 0
0 1 0
1 0 0
1 1 1

The numerical AND operator can be used to test the bit pattern of a number. It can also be used to mask out selected bits of a number. The following table gives some examples of how the AND operator works on two integer numbers.

Expression Explanation
10 And 20 = 0 10 = binary 01010
20 = binary 10100
Hence 10 And 20 = 0

10 And -20 = 8 10 = binary 00000000000000000000000000001010
-20 = binary 11111111111111111111111111101100
Hence 10 And -20 = 8 (binary 1000)

20 And -20 = 4 20 = binary 00000000000000000000000000010100
-20 = binary 11111111111111111111111111101100
Hence 20 And -20 = 4 (binary 100)

Examples

Print True And False
False

Print True And True
True

Print 7, Bin(7, 16)
Print 11, Bin(11, 16)
Print 7 And 11, Bin(7 And 11, 16)
7       0000000000000111
11      0000000000001011
3       0000000000000011

Dim A, B As Boolean

= 10 < 20
= 20 > 30

If A And B Then
  Print "Both A and B are TRUE"
Else
  Print "Either A or B or both are FALSE"
Endif
Either A or B or both are FALSE

See also