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

result := x &^ y

Logical Equivalence

In Go, x &^ y is strictly equivalent to performing a bitwise AND on x and the bitwise complement (NOT) of y.
x &^ y == x & (^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:
Bit x (Left)Bit y (Right)x &^ y (Result)
000
010
101
110
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:
x      = 10101010  (Decimal: 170)
y      = 00001111  (Decimal: 15)

x &^ y = 10100000  (Decimal: 160)
  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

package main

import "fmt"

func main() {
    x := 0b11001100
    y := 0b01010101

    // Perform bit clear
    result := x &^ y

    // %08b formats the integer as an 8-digit binary string
    fmt.Printf("x:      %08b\n", x)
    fmt.Printf("y:      %08b\n", y)
    fmt.Printf("x &^ y: %08b\n", result)
}
Output:
x:      11001100
y:      01010101
x &^ y: 10001000
Master Go with Deep Grasping Methodology!Learn More