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 is a compound assignment operator that performs a bitwise AND operation between the left and right operands, subsequently assigning the resulting value to the left operand. It serves as a syntactic shorthand, meaning x &= y is strictly equivalent to x = x & y, with the exception that the left operand x is evaluated only once.
variable &= expression

Technical Mechanics

  1. Type Constraints: Both operands must resolve to integer types (e.g., int, uint8, int64) or untyped constants representable as integers. Go does not permit bitwise operations on floating-point numbers or complex types.
  2. Bit-by-Bit Evaluation: The operator aligns the binary representations of both operands and evaluates them bit by bit.
  3. Truth Logic: For each corresponding bit position, the resulting bit is set to 1 if and only if both the left and right bits are 1. Otherwise, the resulting bit is set to 0.
Bitwise AND Truth Table:
  • 1 & 1 = 1
  • 1 & 0 = 0
  • 0 & 1 = 0
  • 0 & 0 = 0

Code Example

The following example demonstrates the binary transformation that occurs when the &= operator is applied.
package main

import "fmt"

func main() {
    var a uint8 = 12 // Binary representation: 0000 1100
    var b uint8 = 10 // Binary representation: 0000 1010

    // Perform bitwise AND and assign the result to 'a'
    a &= b 

    // Calculation breakdown:
    //   0000 1100  (12)
    // & 0000 1010  (10)
    // --------
    //   0000 1000  (8)

    fmt.Printf("Decimal: %d | Binary: %08b\n", a, a)
    // Output: Decimal: 8 | Binary: 00001000
}

Evaluation Order

In the expression a[i()] &= b(), the function i() is evaluated exactly once to determine the index of the slice or array, followed by the evaluation of b(). The bitwise AND is then calculated, and the result is written back to the memory address resolved by a[i()].
Master Go with Deep Grasping Methodology!Learn More