TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
<<= (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 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 typeuint(such as the untyped floating-point constant2.0). Ifyis a constant, it must be non-negative. Ifyis evaluated at runtime and is negative, a runtime panic occurs. - Bit Manipulation: The underlying
<<operation shifts the binary representation ofxto the left byypositions. - 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 a1into the MSB will alter the sign of the value.
Syntax Visualization
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





