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.

In Go, the ^ symbol functions as a bitwise operator exclusively for integer types. Its behavior is determined by its arity: it acts as a bitwise XOR (Exclusive OR) when used as a binary operator, and as a bitwise NOT (Complement) when used as a unary operator.

Binary Operation: Bitwise XOR

When placed between two operands, ^ performs a logical exclusive OR operation on each corresponding pair of bits. It yields 1 if the bits are different, and 0 if they are identical.
var a uint8 = 12 // Binary: 00001100
var b uint8 = 10 // Binary: 00001010

result := a ^ b  // Binary: 00000110 (Decimal: 6)

Unary Operation: Bitwise NOT (Complement)

When placed before a single operand, ^ acts as the bitwise complement operator. Go uses ^ for this operation instead of the ~ operator found in C-family languages. It inverts every bit of the operand, changing 0 to 1 and 1 to 0.
var a uint8 = 12 // Binary: 00001100

result := ^a     // Binary: 11110011 (Decimal: 243)
Behavior with Signed Integers: Because Go represents signed integers using two’s complement architecture, applying the unary ^ to a signed integer inverts the sign bit alongside the data bits. Mathematically, ^x on a signed integer evaluates to -(x + 1).
var x int8 = 12  // Binary: 00001100

result := ^x     // Binary: 11110011 (Decimal: -13)

Composite Operator: Bit Clear (&^)

The ^ character is also a constituent of Go’s bit clear (AND NOT) operator, &^. This binary operator clears the bits of the left operand wherever the corresponding bits of the right operand are 1. It is mechanically equivalent to a & (^b).
var a uint8 = 12 // Binary: 00001100
var b uint8 = 10 // Binary: 00001010

result := a &^ b // Binary: 00000100 (Decimal: 4)
Master Go with Deep Grasping Methodology!Learn More