DelphiBasics
And
Keyword
Boolean and or bitwise and of two arguments
1 keyword And(Boolean expression and Boolean expression
2 Integer expression and Integer expression
Description
The And keyword is used in two different ways:
 
1. To perform a logical or boolean 'and' of two logical values. If both are true, then the result is true, otherwise, the result is false.
 
2. To perform a mathematical 'and' of two integers. The result is a bitwise 'and' of the two numbers. For example:
 
10110001 And 01100110 = 00100000
Notes
If the boolean expression is calculated (as opposed to being a Boolean variable), then brackets are required to isolate it.
Related commands
NotBoolean Not or bitwise not of one arguments
OrBoolean or or bitwise or of two arguments
XorBoolean Xor or bitwise Xor of two arguments
 Download this web site as a Windows program.




 
Example code : Illustrate both types of and usage
var
  num1, num2, num3 : Integer;
  letter           : Char;

begin
  num1   := $25;    // Binary value : 0010 0101
  num2   := $32;    // Binary value : 0011 0010
                    // And'ed value : 0010 0000 = $20 = 32 dec
  letter := 'G';

  // And used to return a Boolean value
  if (num1 > 0) And (letter = 'G')
  then ShowMessage('Both values are true')
  else ShowMessage('None or only one true value');

  // And used to perform a mathematical AND operation
  num3 := num1 And num2;

  ShowMessageFmt('$25 And $32 = $%x',[num3]);
end;
Show full unit code
  Both values are true
  $25 And $32 = $20
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page