Difference between revisions of "Xor"

From Lazarus wiki
Jump to navigationJump to search
m (→‎Toggle a bit: Format the inline code.)
 
(8 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 
{{Xor}}
 
{{Xor}}
 +
Back to [[Reserved words]].
 +
 +
The '''xor''' [[Operator|{{HL|operator}}]] compares two [[Boolean|{{HL|boolean}}]]  values, and returns true if and only if one of them is true.
  
 
= Boolean operation =
 
= Boolean operation =
  
 
Exclusive or ('''xor''') results in a value of [[True|true]] if and only if exactly one of the operands has a value of true.
 
Exclusive or ('''xor''') results in a value of [[True|true]] if and only if exactly one of the operands has a value of true.
 
  
 
== Truth table ==
 
== Truth table ==
  
{| border="1" style="border: 1px  solid; border-collapse: collapse;"
+
{| class="wikitable"
 
|-
 
|-
!align=center| A !! align=center|B !!   A xor B  
+
! A !! B !! A xor B
 
|-
 
|-
 
|   false  ||   false  
 
|   false  ||   false  
Line 24: Line 26:
 
|style="background: #eeeeee" |   false
 
|style="background: #eeeeee" |   false
 
|}
 
|}
 
  
 
= Bitwise operation =
 
= Bitwise operation =
  
 
Bitwise xor sets the bit to 1 where the corresponding bits in its operands are different, and to 0 if they are the same.
 
Bitwise xor sets the bit to 1 where the corresponding bits in its operands are different, and to 0 if they are the same.
 +
 +
Example:
 +
 +
    0101'1010
 +
xor 0011'0100
 +
―――――――――――――
 +
    0110'1110
  
 
== Toggle a bit ==
 
== Toggle a bit ==
  
<delphi>
+
<syntaxhighlight lang=pascal>
 
function ToggleBit(const AValue,ABitNumber:integer):integer;
 
function ToggleBit(const AValue,ABitNumber:integer):integer;
 
begin
 
begin
 
   result := AValue xor 1 shl ABitNumber;
 
   result := AValue xor 1 shl ABitNumber;
 
end;
 
end;
</delphi>
+
</syntaxhighlight>
  
If you call ToggleBit(11,0) then get 10. If you call ToggleBit(10,2) then get 14.
+
If you call <syntaxhighlight lang="pascal" inline>ToggleBit(11,0)</syntaxhighlight> then get <syntaxhighlight lang="pascal" inline>10</syntaxhighlight>. If you call <syntaxhighlight lang="pascal" inline>ToggleBit(10,2)</syntaxhighlight> then get <syntaxhighlight lang="pascal" inline>14</syntaxhighlight>.
  
=== Read more ===
+
{{Logical operators}}
 
* [[Variable_parameter#XOR swap| XOR swap]]
 
* [[Variable_parameter#XOR swap| XOR swap]]
* [[Shl]]
 
 
* [[Const]]
 
* [[Const]]
 
* [[Function]]
 
* [[Function]]
 
* [[Integer]]
 
* [[Integer]]
 +
 +
[[Category:Code]]
 +
[[Category:Operators]]

Latest revision as of 05:21, 1 September 2021

Deutsch (de) English (en) suomi (fi) français (fr) русский (ru)
Back to Reserved words.

The xor operator compares two boolean values, and returns true if and only if one of them is true.

Boolean operation

Exclusive or (xor) results in a value of true if and only if exactly one of the operands has a value of true.

Truth table

A B A xor B
  false     false     false
  false   true   true
  true   false   true
  true   true   false

Bitwise operation

Bitwise xor sets the bit to 1 where the corresponding bits in its operands are different, and to 0 if they are the same.

Example:

    0101'1010
xor 0011'0100
―――――――――――――
    0110'1110

Toggle a bit

function ToggleBit(const AValue,ABitNumber:integer):integer;
begin
   result := AValue xor 1 shl ABitNumber;
end;

If you call ToggleBit(11,0) then get 10. If you call ToggleBit(10,2) then get 14.


navigation bar: Pascal logical operators
operators

and • or • not • xor
shl • shr
and_then (N/A)• or_else (N/A)

see also

{$boolEval} • Reference: § “boolean operators” • Reference: § “logical operators”