Difference between revisions of "Xor"
From Lazarus wiki
Jump to navigationJump to searchLine 1: | Line 1: | ||
+ | = 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 == | ||
+ | |||
+ | {| border="1" style="border: 1px solid; border-collapse: collapse;" | ||
+ | |- | ||
+ | !align=center| A !! align=center|B !! A xor B | ||
+ | |- | ||
+ | | false || false | ||
+ | |style="background: #eeeeee" | false | ||
+ | |- | ||
+ | | false || true | ||
+ | |style="background: #eeeeee" | true | ||
+ | |- | ||
+ | | true || false | ||
+ | |style="background: #eeeeee" | true | ||
+ | |- | ||
+ | | true || true | ||
+ | |style="background: #eeeeee" | false | ||
+ | |} | ||
Revision as of 10:46, 26 October 2007
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
<delphi> function ToggleBit(const AValue,ABitNumber:integer):integer; begin
result := AValue xor 1 shl ABitNumber;
end; </delphi>
If you call ToggleBit(11,0) then get 10. If you call ToggleBit(10,2) then get 14.