Number.BitwiseXor
NumberPerforms a bitwise exclusive OR (XOR) operation on two integer values.
Syntax
Number.BitwiseXor(number1 as number, number2 as number) as numberParameters
| Name | Type | Required | Description |
|---|---|---|---|
number1 | number | Yes | The first integer operand. |
number2 | number | Yes | The second integer operand. |
Return Value
number — An integer where each bit is 1 only if the corresponding bits differ between the two inputs.
Remarks
Number.BitwiseXor performs a bitwise exclusive OR (XOR). A bit in the result is 1 when the corresponding bits in the two operands differ — one is 0 and the other is 1. When both bits are the same (both 0 or both 1), the result bit is 0.
XOR has a key algebraic property: applying XOR twice with the same mask is self-inverse — a XOR b XOR b = a. This makes it useful for:
- Toggling bits: Number.BitwiseXor(flags, mask) flips the mask bits regardless of their current state (unlike OR which only sets, or AND-NOT which only clears)
- Comparing two values bit-by-bit: the result bits show exactly which bits differ
- Simple reversible encoding: XOR encoding and decoding with the same key
XOR is the bitwise operation most sensitive to the "difference" between two integers — Number.BitwiseXor(x, x) = 0 (a number XORed with itself is always 0), and Number.BitwiseXor(x, 0) = x (XOR with 0 is identity).
Examples
Example 3: XOR is self-inverse — encoding and decoding with the same mask
let
Original = 42,
Mask = 15,
Encoded = Number.BitwiseXor(Original, Mask),
Decoded = Number.BitwiseXor(Encoded, Mask)
in
#table({"Original", "Encoded", "Decoded"}, {{Original, Encoded, Decoded}})The final output — a single-row table showing Original = 42, Encoded = 37, and Decoded = 42.
Original | Encoded | Decoded | |
|---|---|---|---|
| 1 | 42 | 37 | 42 |