CKTIS / C
Bitwise XOR assignment (^=)
The ^= operator computes bitwise XOR, converts the result to the type of the left operand, and stores it there.
Version: C23; repository compiler flag, production runtime not attested
Syntax
lhs ^= rhslhs ^= rhs computes bitwise XOR and stores the result in lhs. This is not exponentiation.
Both operands must have integer types, and lhs must be a modifiable lvalue. Integer promotions and the usual arithmetic conversions apply. In the XOR calculation itself, a 1 bit in the converted mask flips the corresponding bit of the converted left operand; a 0 bit leaves it unchanged. Before storage, the result is converted to the type of lhs, so this rule need not hold for the stored value. For example, given _Bool flag = 1, flag ^= 3u computes 2u, which converts to 1 when assigned to _Bool.
For a non-atomic lhs, lhs ^= rhs works like lhs = lhs ^ (rhs), except that the left operand is evaluated only once. For an atomic lhs, ^= still evaluates the left operand only once and performs a single sequentially consistent atomic read-modify-write. In contrast, lhs = lhs ^ (rhs) performs a separate atomic load and atomic store. Use unsigned types for masks. In the example, 10u ^ 6u produces 12u; applying the same mask again restores 10u.
Examples
#include <stdio.h>
int main(void)
{
unsigned int flags = 10u;
unsigned int mask = 6u;
flags ^= mask;
printf("%u\n", flags);
flags ^= mask;
printf("%u\n", flags);
return 0;
}
Result
12 10
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.