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 <<= (left shift assignment) operator is a compound assignment statement in Go that performs a bitwise left shift on the left operand by the number of bit positions specified by the right operand, and subsequently assigns the computed result back to the left operand.
x <<= y
In Go, x <<= y is a statement, not an expression. It does not evaluate to or return a value, and therefore cannot be embedded within other expressions. While functionally similar to the expanded assignment x = x << y, the Go specification dictates that in a compound assignment, the left operand x is evaluated exactly once. This creates a significant semantic difference if x contains side effects. For example, the statement arr[i()] <<= y evaluates the function i() only once, whereas arr[i()] = arr[i()] << y would evaluate i() twice.

Technical Mechanics

  • Operand Constraints: The left operand (x) must be an assignable expression of an integer type (signed or unsigned). This includes addressable values (such as variables) as well as map index expressions (which are assignable but explicitly not addressable). The right operand (y), representing the shift count, must be of an integer type or an untyped constant representable by a value of type uint (such as the untyped floating-point constant 2.0). If y is a constant, it must be non-negative. If y is evaluated at runtime and is negative, a runtime panic occurs.
  • Bit Manipulation: The underlying << operation shifts the binary representation of x to the left by y positions.
  • Zero Padding: As bits are shifted to the left, the vacated bit positions on the right are padded with zeroes.
  • Truncation and Overflow: Any bits shifted beyond the most significant bit (MSB) boundary of the left operand’s specific bit-width (e.g., beyond the 8th bit in a uint8) are permanently discarded. For signed integers, shifting a 1 into the MSB will alter the sign of the value.

Syntax Visualization

package main

import "fmt"

func main() {
    var val uint8 = 5 // Binary representation: 00000101
    
    // Shift the bits of 'val' left by 3 positions and assign back to 'val'.
    // The right operand can be an untyped constant representable as a uint.
    val <<= 3.0         
    
    // The binary representation is now: 00101000
    // Decimal equivalent: 40
    fmt.Println(val) 
    
    // Example with a map index expression (assignable, but not addressable)
    m := map[string]int{"key": 2}
    m["key"] <<= 2 
    fmt.Println(m["key"]) // Outputs: 8
}

Type Evaluation

The <<= operator does not alter the type of the left operand. The evaluation type of the underlying shift operation is strictly dictated by the type of the left operand. The statement updates x with a computed value of the exact same type as x, regardless of the type or magnitude of the shift count y.
Master Go with Deep Grasping Methodology!Learn More