Skip to main content
The ^= operator in Go is the bitwise XOR (exclusive OR) assignment operator. It performs a bitwise XOR operation between the left and right integer operands, assigning the computed result back to the left operand. Syntactically, it is a shorthand compound assignment. The expression:
Is logically equivalent to:

Bitwise Evaluation Logic

The operator evaluates the operands at the binary level, comparing them bit by bit. For each corresponding bit position, the XOR logic dictates:
  • Returns 1 if the bits are different (one is 0 and the other is 1).
  • Returns 0 if the bits are identical (both are 0 or both are 1).
Truth Table:
  • 0 ^ 0 = 0
  • 0 ^ 1 = 1
  • 1 ^ 0 = 1
  • 1 ^ 1 = 0

Type Constraints

In Go, the ^= operator strictly requires both operands to be of integer types (e.g., int, uint8, int32, byte, rune). Attempting to use this operator on floating-point numbers, strings, or booleans will result in a compile-time type mismatch error.

Execution Example

The following example demonstrates the mechanical bit-level mutation of a variable using ^=:
Output:
Tired of Poor Go Skills? Fix That With Deep Grasping!Learn More