Sign in Join
Wiki copytemporary

Xor: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11Back to [[Reserved words]].
22
33The **xor** [[Operator|operator]] compares two [[Boolean|boolean]] values, and returns true if and only if one of them is true.
44
55# Boolean operation
66
77Exclusive or (**xor**) results in a value of [[True|true]] if and only if exactly one of the operands has a value of true.
88
99## Truth table
1010
1111| A | B | A xor B |
1212|-----------|-----------|---------|
1313|   false   |   false   |   false |
1414|   false |   true |   true |
1515|   true |   false |   true |
1616|   true |   true |   false |
1717
1818# Bitwise operation
1919
2020Bitwise xor sets the bit to 1 where the corresponding bits in its operands are different, and to 0 if they are the same.
2121
2222Example:
2323
24-`    0101'1010`\
25-`xor 0011'0100`\
26-`―――――――――――――`\
27-`    0110'1110`
24+```text
25+ 0101'1010
26+xor 0011'0100
27+―――――――――――――
28+ 0110'1110
29+```
2830
2931## Toggle a bit
3032
3133``` pascal
3234function ToggleBit(const AValue,ABitNumber:integer):integer;
3335begin
3436 result := AValue xor 1 shl ABitNumber;
3537end;
3638```
3739
3840If you call `ToggleBit(11,0)` then get `10`. If you call `ToggleBit(10,2)` then get `14`.
3941
4042- [[Variable parameter|XOR swap]]
4143- [[Const]]
4244- [[Function]]
4345- [[Integer]]
4446
4547---
4648*From the Free Pascal wiki, page "Xor", as it stood on 2021-09-01 after a change by Mozi, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
4749Category: Code
4850Category: Operators

Back to history