C Bitwise XOR Assignment Operator

In C, the Bitwise XOR Assignment ^= operator is a compound assignment operator that performs a bitwise XOR operation between two operands and assigns the result to the left operand.

Bitwise XOR Assignment operator is useful for toggling bits in a variable efficiently.


Syntax of the Bitwise XOR Assignment Operator

The syntax to use the Bitwise XOR Assignment operator is:

</>
Copy
variable ^= value;

Explanation:

  1. variable: The left operand whose bits will be XORed.
  2. ^=: The Bitwise XOR Assignment operator.
  3. value: The right operand whose bits are XORed with variable.
  4. Return Value: The operator returns the updated value of variable after the XOR operation.

Examples of the Bitwise XOR Assignment Operator

1. Using XOR Assignment on Two Integers

In this example, we will use the ^= operator to perform a bitwise XOR operation on two integers and print the result.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int a = 5;  // Binary: 0101
    int b = 3;  // Binary: 0011

    a ^= b; // a = a ^ b

    printf("Result after XOR Assignment: %d\n", a);
    
    return 0;
}

Explanation:

  1. We declare two integer variables a and b with values 5 and 3.
  2. The binary representation of a is 0101 and b is 0011.
  3. The expression a ^= b is equivalent to a = a ^ b.
  4. The XOR operation results in 0110 (decimal 6), which is assigned back to a.

Output:

Result after XOR Assignment: 6

2. Using XOR Assignment to Toggle a Bit

In this example, we will use the ^= operator to toggle a specific bit in an integer.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num = 6;   // Binary: 0110
    int mask = 2;  // Binary: 0010

    num ^= mask;  // Toggle the second bit

    printf("Number after bit toggle: %d\n", num);

    return 0;
}

Explanation:

  1. We initialize num with the value 6 (binary 0110).
  2. We define mask as 2 (binary 0010), which represents the bit we want to toggle.
  3. The operation num ^= mask flips the second bit of num.
  4. The resulting value of num is 0100 (decimal 4).

Output:

Number after bit toggle: 4

3. Swapping Two Numbers Using XOR Assignment

In this example, we will use the ^= operator to swap two numbers without using a temporary variable.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int x = 10, y = 20;

    x ^= y;
    y ^= x;
    x ^= y;

    printf("After swapping: x = %d, y = %d\n", x, y);
    
    return 0;
}

Explanation:

  1. We initialize two integers x and y with values 10 and 20.
  2. The first XOR operation x ^= y stores the XOR result in x.
  3. The second XOR operation y ^= x extracts the original value of x and stores it in y.
  4. The third XOR operation x ^= y extracts the original value of y and stores it in x.
  5. As a result, x and y are swapped without using a temporary variable.

Output:

After swapping: x = 20, y = 10

Conclusion

In this tutorial, we explored the Bitwise XOR Assignment ^= operator in C:

  1. The ^= operator performs a bitwise XOR between two values and assigns the result to the left operand.
  2. It is useful for toggling bits, performing XOR-based operations, and swapping values.
  3. The return value of ^= is the updated value of the left operand.