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 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:
x ^= y
Is logically equivalent to:
x = x ^ y

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 ^=:
package main

import "fmt"

func main() {
    var x uint8 = 10 // Binary representation: 0000 1010
    var y uint8 = 6  // Binary representation: 0000 0110

    // Perform bitwise XOR and assign the result to x
    x ^= y 

    // Bitwise breakdown:
    //   0000 1010  (x)
    // ^ 0000 0110  (y)
    // --------
    //   0000 1100  (Result: 12 in decimal)

    fmt.Printf("Decimal: %d\n", x)
    fmt.Printf("Binary:  %08b\n", x)
}
Output:
Decimal: 12
Binary:  00001100
Master Go with Deep Grasping Methodology!Learn More