Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Operator ^=

Toggles precisely the stored bits selected by the mask on the right.

Version: Python 3.14; repository image, production runtime not attested

Syntax

target ^= value

Toggles precisely the stored bits selected by the mask on the right.

Use exclusive OR assignment to toggle the bits selected by an integer mask. Bits selected by zeroes in the mask retain their original values.

For mutable sets, the corresponding operation replaces the contents with their symmetric difference. Applying the same integer mask twice restores the initial value.

Applying the same exclusive-or mask twice restores the original bits.

Syntax

target ^= value

__ixor__, __xor__, __rxor__ · TypeError

Worked example

mask = 0b1100; mask ^= 0b0110
mask → 0b1010
mask ^ 0b0110 → 0b1100

Examples

value = 0b1100
value ^= 0b1010
print(value)
print(bin(value ^ 0b1010))

Result

6
0b1100

Applying the same exclusive-or mask twice restores the original bits.

Sources