Difference between revisions of "Xor"

From Lazarus wiki
Jump to navigationJump to search
m
m (Fixed syntax highlighting)
Line 1: Line 1:
 
{{Xor}}
 
{{Xor}}
 +
 +
 +
Back to [[Reserved words]].
 +
  
 
= 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 ==
Line 24: Line 27:
 
|style="background: #eeeeee" |   false
 
|style="background: #eeeeee" |   false
 
|}
 
|}
 
  
 
= Bitwise operation =
 
= Bitwise operation =
Line 32: Line 34:
 
== Toggle a bit ==
 
== Toggle a bit ==
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
function ToggleBit(const AValue,ABitNumber:integer):integer;
 
function ToggleBit(const AValue,ABitNumber:integer):integer;
 
begin
 
begin
Line 42: Line 44:
  
 
== See also ==
 
== See also ==
 +
 
* [[Variable_parameter#XOR swap| XOR swap]]
 
* [[Variable_parameter#XOR swap| XOR swap]]
 
* [[Shl]]
 
* [[Shl]]
Line 47: Line 50:
 
* [[Function]]
 
* [[Function]]
 
* [[Integer]]
 
* [[Integer]]
<br>
 
<br>
 

Revision as of 08:16, 3 March 2020

Deutsch (de) English (en) suomi (fi) français (fr) русский (ru)


Back to Reserved words.


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.

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.

See also