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