Name
And Keyword
Syntax
Boolean expressionandBoolean expressionInteger expressionandInteger expression
Description
The
and operator performs a logical
and if the operators are of Boolean type or a
bitwise and if the operators are integers.
Integer operands can be of any integer type, including
Int64. A logical and is False
if either operand is False and is True if both operands are True.
Tips and Tricks
Unlike standard Pascal, if the left-hand operand is False, Delphi does not evaluate the right-hand operand because the result must be False. You can avoid this shortcut operation and return to standard Pascal with the
$BoolEvalor$Bcompiler directives.An integer
andoperates on each bit of its operands, setting the result bit to zero if either operand has a zero bit, and sets a bit to one if both operands have 1 bits. If one operand is smaller than the other, Delphi extends the smaller operand with zero in the leftmost bits. The result is the size of the largest operand.
Examples
var
I, J: Integer;
S: string;
begin
I := $F0;
J := $8F;
WriteLn(I and J); // Writes 128 (which is $80)
// The short-circuit behavior of AND in the next example prevents
// Delphi from referring to the nonexistent string element, S[1].
S := '';
if (Length(S) > 0) and (S[1] = 'X') then
Delete(S, 1, 1);See Also
| Boolean Type, ByteBool Type, LongBool Type, Not Keyword, Or Keyword, Shl Keyword, Shr Keyword, WordBool Type, |
| Xor Keyword, $B Compiler Directive, $BoolEval Compiler Directive |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access