TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
^ 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:| Bit 1 | Bit 2 | Result (Bit 1 ^ Bit 2) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
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:
- Integral Promotions: Operands smaller than
int(likecharorshort) are promoted tointorunsigned int. - 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 (
|)
==, !=), parentheses are strictly required when combining bitwise XOR with relational evaluations.
Master C++ with Deep Grasping Methodology!Learn More





