Logical, Relational and Bitwise Operators

1 Logical operators include: and, or, not, xor

These operators take boolean values as operands and also return boolean results;

When both operands are integers, and/or/not/xor become bitwise operators and produce integer results.

2 Relational (comparison) operators: =, <>, >, <, <=, >=.

If you are familiar with C-family languages, pay close attention to these differences:

Pascal uses a single equals sign for equality checks: A = B (C languages use A == B);

Pascal’s dedicated assignment operator is :=.

Logical / bitwise operators have higher precedence than relational operators.

Parentheses must wrap certain expressions to control evaluation order; otherwise logic errors or compilation failures will occur.

Incorrect example (compiler error)

var
  A, B: Integer;
begin
  if A = 0 and B <> 0 then ... // Wrong syntaxCode language: JavaScript (javascript)

Reason for compilation failure:

Compiler precedence rules execute the middle bitwise AND 0 and B first (resulting in an integer);

Then compute A = (0 and B), yielding a boolean value;

The program then attempts to compute between a boolean and integer 0, triggering a type mismatch error.

Correct syntax

var
  A, B: Integer;
begin
  if (A = 0) and (B <> 0) then ...Code language: HTML, XML (xml)

Short-circuit evaluation rules

Take the expression if MyFunction(X) and MyOtherFunction(Y) then... as an example, rules are as follows:

  1. and operation: Evaluate left-hand side MyFunction(X) first
    • If the left side returns false, the entire expression is guaranteed false, and the right-hand function MyOtherFunction(Y) will not execute at all.
  2. or operation works similarly:
    • If the left side already returns true, the overall result is true, and right-side code skips execution.

This feature is extremely useful for null pointer checks:

if (A <> nil) and A.IsValid then...Code language: HTML, XML (xml)

The code will not crash even if A is the null pointer nil. Keyword nil represents null pointer, numerically equivalent to 0, corresponding to null in other languages.

Operator Classification

Relational Operators (return Boolean)

PascalMeaningC Equivalent
=Equal to==
<>Not equal to!=
>Greater than>
<Less than<
>=Greater than or equal to>=
<=Less than or equal to<=

Logical / Bitwise Operators

not: Logical NOT / Bitwise NOT and: Logical AND / Bitwise AND or: Logical OR / Bitwise OR xor: Logical XOR / Bitwise XOR

not (NOT)

anot a
truefalse
falsetrue

and (AND, true only when both operands true)

aba and b
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

or (OR, true if at least one operand true)

aba or b
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

xor (XOR, true when operands differ)

aba xor b
truetruefalse
truefalsetrue
falsetruetrue
falsefalsefalse

Examples

Leap Year Judgment Rules

  1. Divisible by 4, but not divisible by 100 → Leap year
  2. Divisible by 400 → Leap year All others are common years
{$ifdef FPC} {$mode objfpc} {$H+} {$endif}
{$codepage utf8}
program LeapYearCheck;

// Function to judge leap year, input year, return Boolean
function IsLeapYear(year: Integer): Boolean;
begin
  Result := ((year mod 4 = 0) and (year mod 100 <> 0)) or (year mod 400 = 0);
end;

var
  y: Integer;
begin
  Write('Enter year: ');
  ReadLn(y);

  if IsLeapYear(y) then
    Writeln(y, ' is a leap year')
  else
    Writeln(y, ' is a common year');

  ReadLn;
end.Code language: JavaScript (javascript)
  • mod remainder operator, year mod 4 = 0 means divisible by 4
  • Expressions must use parentheses: and has higher precedence than comparison operators
  • Short-circuit feature: If the or prior condition of divisible by 400 is satisfied, the 4/100 conditions will not be evaluated

Logical, Relational and Bitwise Operators

Leave a Reply

Your email address will not be published. Required fields are marked *