Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

xor — alternative spelling of bitwise exclusive OR

In C++, `xor` is an alternative spelling of the `^` operator. The built-in operator compares corresponding operand bits: a result bit is 1 exactly when the input bits differ. No additional header is required.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

lhs xor rhs

lhs xor rhs means the same as lhs ^ rhs: the spelling xor preserves the semantics, precedence, and associativity of ^. It is a reserved language token, not a function or library macro; you cannot use it as a variable name. The operator itself requires no header.

The built-in operator accepts operands of integral or unscoped enumeration type (unscoped enum). It performs the usual arithmetic conversions, including the applicable integral promotions. Each result bit is 1 if exactly one corresponding bit of the converted operands is 1; otherwise it is 0. For example, 0b1100u xor 0b1010u produces 0b0110u, or 6. This is not exponentiation.

xor is a bitwise operator, not a short-circuit logical operator: both operands are evaluated. With bool operands, promotions make the built-in operator return int, not bool. If you need a logical condition meaning “exactly one is true,” compare two bool values using !=.

The operator groups left-to-right. Its precedence is lower than & and ==, but higher than |; use parentheses in mixed expressions, such as (a xor b) == 6u. xor_eq is a separate token corresponding to ^=: unlike plain xor, it also performs assignment. The example uses unsigned constants and static_assert checks. The eel.is source is an evolving working draft, not a frozen C++23 publication.

Examples

#include <iostream>

int main() {
    constexpr unsigned a = 0b1100u;
    constexpr unsigned b = 0b1010u;
    constexpr unsigned result = a xor b;
    static_assert(result == 0b0110u);
    static_assert(result == (a ^ b));
    std::cout << result << '\n';
}

Result

6

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources