Skip to main content
The ^ operator in C++ is the bitwise Exclusive OR (XOR) operator. It performs a logical XOR operation on each pair of corresponding bits of its two integer operands, returning a new value where each bit is set to 1 if the operand bits differ, and 0 if they are identical.

Bit-Level Mechanics

The operator evaluates the binary representation of the operands strictly bit-by-bit according to the following truth table:

Type Requirements and Conversions

The ^ operator requires both operands to be of integral types (e.g., int, char, short, long, unsigned) or unscoped enumeration types. It cannot be applied to floating-point types. Before the bitwise operation is executed, C++ applies standard type conversions:
  1. Integral Promotions: Operands smaller than int (like char or short) are promoted to int or unsigned int.
  2. Usual Arithmetic Conversions: If the operands are of different types after promotion, they are converted to a common type (typically the larger or unsigned type) to ensure bit-width alignment.

Evaluation Example

Compound Assignment

C++ provides the ^= compound assignment operator, which applies the bitwise XOR operation and assigns the result directly to the left operand.

Operator Precedence

In the C++ operator precedence hierarchy, the bitwise XOR operator (^) sits:
  • Below the bitwise AND operator (&)
  • Above the bitwise OR operator (|)
It has left-to-right associativity. Because its precedence is lower than equality operators (==, !=), parentheses are strictly required when combining bitwise XOR with relational evaluations.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More