Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Bitwise exclusive OR operator (^)

The ^ operator computes XOR for corresponding bits: a result bit is 1 exactly when the operand bits differ. It is useful for toggling selected bits with a mask; it does not mean exponentiation.

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

Syntax

lhs ^ rhs

The built-in ^ operator performs bitwise exclusive OR: 0 ^ 0 and 1 ^ 1 produce 0, while 0 ^ 1 and 1 ^ 0 produce 1. It is not an exponentiation operator. The token xor is an alternative spelling of the same operator.

Operands must have integral or unscoped enumeration type (unscoped enum). The usual arithmetic conversions apply, including integral promotions; the common converted type is the result type. The result therefore need not have the original operands' type. Built-in ^ does not accept floating-point types or enum class.

The operator groups left-to-right: a ^ b ^ c means (a ^ b) ^ c. Its precedence is lower than & but higher than |; use parentheses in mixed bitwise expressions.

In the example, value ^ mask produces 0b0110, or 6. Ones in mask toggle the corresponding bits, while zeros leave them unchanged. Applying the same mask again restores value. The eel.is source is an evolving working draft, not a frozen C++23 publication.

Examples

#include <iostream>

int main() {
    constexpr unsigned value = 0b1100u;
    constexpr unsigned mask = 0b1010u;
    constexpr unsigned result = value ^ mask;
    static_assert(result == 6u);
    static_assert((result ^ mask) == value);
    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