Xor: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Xor
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Back to [[Reserved words]]. | |
| 2 | + | ||
| 3 | + | The **xor** [[Operator|operator]] compares two [[Boolean|boolean]] values, and returns true if and only if one of them is true. | |
| 4 | + | ||
| 5 | + | # Boolean operation | |
| 6 | + | ||
| 7 | + | Exclusive or (**xor**) results in a value of [[True|true]] if and only if exactly one of the operands has a value of true. | |
| 8 | + | ||
| 9 | + | ## Truth table | |
| 10 | + | ||
| 11 | + | | A | B | A xor B | | |
| 12 | + | |-----------|-----------|---------| | |
| 13 | + | | false | false | false | | |
| 14 | + | | false | true | true | | |
| 15 | + | | true | false | true | | |
| 16 | + | | true | true | false | | |
| 17 | + | ||
| 18 | + | # Bitwise operation | |
| 19 | + | ||
| 20 | + | Bitwise xor sets the bit to 1 where the corresponding bits in its operands are different, and to 0 if they are the same. | |
| 21 | + | ||
| 22 | + | Example: | |
| 23 | + | ||
| 24 | + | ` 0101'1010`\ | |
| 25 | + | `xor 0011'0100`\ | |
| 26 | + | `―――――――――――――`\ | |
| 27 | + | ` 0110'1110` | |
| 28 | + | ||
| 29 | + | ## Toggle a bit | |
| 30 | + | ||
| 31 | + | ``` pascal | |
| 32 | + | function ToggleBit(const AValue,ABitNumber:integer):integer; | |
| 33 | + | begin | |
| 34 | + | result := AValue xor 1 shl ABitNumber; | |
| 35 | + | end; | |
| 36 | + | ``` | |
| 37 | + | ||
| 38 | + | If you call `ToggleBit(11,0)` then get `10`. If you call `ToggleBit(10,2)` then get `14`. | |
| 39 | + | ||
| 40 | + | - [[Variable parameter|XOR swap]] | |
| 41 | + | - [[Const]] | |
| 42 | + | - [[Function]] | |
| 43 | + | - [[Integer]] | |
| 44 | + | ||
| 45 | + | --- | |
| 46 | + | *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).* | |
| 47 | + | Category: Code | |
| 48 | + | Category: Operators |
