Name

Xor Keyword

Syntax

Boolean expression xor Boolean expression
Integer expression xor Integer expression

Description

The xor operator performs an exclusive or on its operands. If the operands are of Boolean type, it returns a Boolean result: True if the operands are different and False if they are the same.

An integer xor operates on each bit of its operands, setting the result bit to 1 if the corresponding bits in both operands are different, and to if both operands have identical bits. If one operand is smaller than the other, Delphi extends the smaller operand with in the leftmost bits. The result is the size of the largest operand.

Tips and Tricks

  • With Boolean operands, xor is just like comparing for inequality. In most cases, the <> operator is easier to understand than using xor.

  • The xor operation is reversible, which leads some people to use it for encrypting passwords. Using xor to encrypt passwords is like using bubble gum to lock a door. It doesn’t work, and it’s easy to break in. Search the World Wide Web to find free, reusable solutions for securely encrypting passwords.

Examples

var
  I, J: Integer;
begin
  I := $25;
  J := $11;
  WriteLn(I xor J); // Writes 52 (which is $34)
...

See Also

And Keyword, Boolean Type, ByteBool Type, LongBool Type, Not Keyword, Or Keyword, Shl Keyword, Shr Keyword, WordBool Type, Xor Keyword, $B Compiler Directive, $BoolEval Compiler Directive

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.