Skip to main content
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.

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.
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).

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).
Tired of Poor Go Skills? Fix That With Deep Grasping!Learn More