Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

xor_eq — bitwise XOR assignment

`xor_eq` is an alternative spelling of the C++ `^=` operator. The built-in operator computes bitwise exclusive OR, then converts the result to the left operand’s type before storing it. In the `unsigned int` example, ones in the mask toggle the corresponding bits, and zeros leave them unchanged.

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

Syntax

lhs xor_eq rhs; // equivalent spelling: lhs ^= rhs;

xor_eq is a reserved alternative spelling of ^=, not a function or macro. Both spellings denote the same operator; no header is needed to use xor_eq. You cannot use this name as a variable identifier. Distinguish xor_eq, which stores its result, from xor and ^, which do not themselves perform assignment.

For the built-in operator, lhs xor_eq rhs performs bitwise XOR and stores the result in lhs. Each result bit is 1 exactly when the corresponding operand bits differ. The left operand must be a modifiable lvalue of integral type; the right operand must have integral or unscoped enumeration type. Integral promotions and the usual arithmetic conversions apply, and the result is converted to the left operand's type.

The built-in assignment behaves like lhs = lhs ^ rhs, except that it evaluates lhs only once. XOR operates on individual bits, not as a logical test of whole values. This description concerns the built-in operator; for class types, an overloaded operator^= can define the behavior.

In the example, 0b1100u and 0b1010u produce 0b0110u, which is 6. Applying the same mask again restores 12: mask bits set to one toggle the corresponding bits, while zero bits leave them unchanged.

Examples

#include <iostream>

int main() {
    unsigned int flags = 0b1100u;
    const unsigned int mask = 0b1010u;

    flags xor_eq mask;
    std::cout << flags << '\n';

    flags xor_eq mask;
    std::cout << flags << '\n';
}

Result

6
12

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