[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.7 and

Synopsis

 
operator and (operand1, operand2: Boolean) = Result: Boolean;
or
 
operator and (operand1, operand2: integer type) = Result: integer type;
or
 
procedure and (var operand1: integer type; operand2: integer type);

Description

In GNU Pascal, `and' has three built-in meanings:

  1. Logical "and" between two `Boolean'-type expressions. The result of the operation is of `Boolean' type.

    By default, `and' acts as a short-circuit operator in GPC: If the first operand is `False', the second operand is not evaluated because the result is already known to be `False'. You can change this to complete evaluation using the `--no-short-circuit' command-line option or the `{$B+}' compiler directive.

  2. Bitwise "and" between two integer-type expressions. The result is of the common integer type of both expressions.

  3. Use as a "procedure": `operand1' is "and"ed bitwise with `operand2'; the result is stored in `operand1'.

Conforming to

The logical `and' operator is defined in ISO-7185 Pascal.

According to ISO, you cannot rely on `and' being a short-circuit operator. On the other hand, GPC's default behaviour does not contradict the ISO standard. (See section 9.9 and_then.) However, since it seems to be a de-facto standard among ISO Pascal compilers to evaluate both operands of `and', GPC switches to `--no-short-circuit' mode if one of the language dialect options selecting ISO Pascal, for instance `--extended-pascal', is given. Use `--short-circuit' to override.

Use of `and' as a bitwise operator for integers is a Borland Pascal extension.

Use of `and' as a "procedure" is a GNU extension.

Example

 
program AndDemo;
var
  a, b, c: Integer;
begin
  if (a = 0) and (b = 0) then  { logical `and' }
    c := 1
  else if a and b = 0 then  { bitwise `and' }
    c := 2
  else
    and (c, a)  { same as `c := c and a' }
end.

Note the difference between the logical `and' and the bitwise `and': When `a' is 2 and `b' is 4, then `a and b' is 0. Beware: `a and b = 0' has nothing to do with `(a = 0) and (b = 0)'!

Since bitwise `and' has a higher priority than the `=' operator, parentheses are needed in `if (a = 0) and (b = 0)' because otherwise `0 and b' would be calculated first, and the remainder would cause a parse error.

See also

section 9.9 and_then, section 9.8 and then, section 9.180 or, section 9.308 xor, section 8.3 Operators.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Frank Heckenbach on May, 10 2002 using texi2html