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 bitwise left shift operator. It shifts the binary representation of its left operand to the left by the number of bit positions specified by its right operand.
result := x << y

Mechanics

When a left shift operation occurs:
  1. The bits of the left operand (x) are moved to the left by y positions.
  2. The vacated bit positions on the right (least significant bits) are filled with zeroes.
  3. The bits that are shifted past the boundary of the data type’s most significant bit (leftmost edge) are permanently discarded.
  4. Mathematically, x << y is equivalent to x×2yx \times 2^y, truncated to the bit-width of the left operand’s type.
var x uint8 = 13 // Binary: 00001101 (Decimal: 13)
var y = x << 2   // Binary: 00110100 (Decimal: 52)

Operand Type Constraints

Go enforces strict typing rules for bitwise shift operands:
  • Left Operand (x): Must be an integer type (signed or unsigned) or an untyped integer constant. The type of the left operand dictates the type of the evaluated result.
  • Right Operand (y): Must be an integer type. The value of the right operand must be non-negative at runtime. Attempting to shift by a negative value results in a compile-time error (if constant) or a run-time panic.

Overflow Behavior

Because Go fixed-width integers have strict boundaries, left shifting frequently results in integer overflow. Go handles this by silently discarding the high-order bits that exceed the type’s capacity.
var a uint8 = 128 // Binary: 10000000
var b = a << 1    // Binary: 00000000 (Decimal: 0) - The 1 is discarded

Untyped Constants

Go’s shift operators exhibit specific behavior when interacting with untyped constants, depending entirely on whether the right operand is a constant or a variable:
  • Constant Shift Expressions: If the left operand is an untyped constant and the right operand is also a constant, the shift operation is evaluated at compile-time with arbitrary precision. The intermediate result is not constrained by standard integer limits (like 64-bit boundaries) until it is explicitly assigned to a typed variable.
  • Non-Constant Right Operand: If the right operand is a variable, the untyped constant left operand is implicitly converted to its default type (typically int) before the shift occurs. The operation is then evaluated at runtime and is subject to standard fixed-width integer boundaries.
// Constant shift expression: Evaluated at compile-time with arbitrary precision.
// 1 is shifted left by 100 positions, exceeding 64-bit limits, but valid here.
const huge = 1 << 100 

// The compiler evaluates this by shifting right again, 
// resulting in 1, which fits perfectly into a standard int.
var normal int = huge >> 100 

// Non-constant right operand: Evaluated at runtime.
var shift uint = 64
// The untyped constant '1' implicitly converts to 'int' before shifting.
// On a 64-bit architecture, shifting an int by 64 overflows and results in 0.
var runtimeShift = 1 << shift 
Master Go with Deep Grasping Methodology!Learn More