Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The ^ operator in C is the bitwise Exclusive OR (XOR) operator. It performs a logical XOR operation on each corresponding pair of bits from two integral operands. The operator evaluates to 1 if the compared bits are different, and 0 if they are identical.
result = operand1 ^ operand2;

Bitwise Evaluation

The operator evaluates operands at the binary level according to the following truth table:
Bit ABit BA ^ B
000
011
101
110

Technical Constraints and Behavior

  • Type Restrictions: The ^ operator requires both operands to be of integral types (e.g., char, short, int, long, unsigned). Attempting to use floating-point types (float, double) will result in a compilation error.
  • Integer Promotion: Before the bitwise operation occurs, C applies standard integer promotions. Operands with types smaller than int (like char or short) are implicitly promoted to int or unsigned int. The operation is then performed on the promoted types.
  • Compound Assignment: C supports the ^= compound assignment operator, which applies the bitwise XOR and assigns the result to the left operand (a ^= b is semantically equivalent to a = a ^ b).

Mathematical Properties

The bitwise XOR operator adheres to several strict algebraic properties:
  • Identity: A ^ 0 == A (XORing a value with zero leaves the value unchanged).
  • Self-Inverse: A ^ A == 0 (XORing a value with itself results in zero).
  • Commutativity: A ^ B == B ^ A (The order of operands does not affect the result).
  • Associativity: (A ^ B) ^ C == A ^ (B ^ C) (The grouping of operands does not affect the result).

Execution Example

The following code demonstrates the bit-by-bit evaluation of the ^ operator:
#include <stdio.h>

int main() {
    unsigned char a = 12; 
    unsigned char b = 10; 
    
    unsigned char result = a ^ b; 
    
    /* 
       Binary Evaluation:
         0000 1100  (a = 12)
       ^ 0000 1010  (b = 10)

         0000 0110  (result = 6)
    */
    
    printf("Result: %d\n", result); // Outputs: Result: 6
    
    return 0;
}
Master C with Deep Grasping Methodology!Learn More