Skip to main content
The &^ operator in Go is the bit clear (AND NOT) operator. It is a binary operator that forces bits in the left operand to 0 if the corresponding bits in the right operand are set to 1. If a bit in the right operand is 0, the corresponding bit in the left operand remains unchanged.

Syntax

Logical Equivalence

In Go, x &^ y is strictly equivalent to performing a bitwise AND on x and the bitwise complement (NOT) of y.
Go provides &^ as a dedicated operator to perform this operation in a single step, avoiding the need to manually invert the right operand before applying the AND operation.

Truth Table

The operation evaluates each bit position independently according to the following logic: Rule of thumb: The right operand acts as a mask. A 1 in the mask clears the bit. A 0 in the mask leaves the bit alone.

Bitwise Mechanics

To visualize the operation, align the binary representations of two 8-bit integers:
  1. The upper 4 bits of y are 0. Therefore, the upper 4 bits of x (1010) are preserved in the result.
  2. The lower 4 bits of y are 1. Therefore, the lower 4 bits of x (1010) are forced to 0000 in the result.

Code Implementation

Output:
Tired of Poor Go Skills? Fix That With Deep Grasping!Learn More