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 shifts the binary representation of its left operand to the left by the number of bits specified by its right operand, and assigns the resulting value back to the left operand.

Syntax

x <<= y;
This expression is semantically equivalent to:
x = (T)(x << y);
where T is the data type of x. The explicit cast is conceptually required because C# applies numeric promotion to operands smaller than int (such as byte, sbyte, short, ushort, and char), implicitly converting them to int before the shift operation occurs. The result must then be cast back to the original type T. Additionally, in the case of <<=, the left operand x is evaluated only once, which is significant if x is an expression with side effects.

Bitwise Mechanics

When the <<= operator executes:
  1. The bits of the left operand are shifted to the left.
  2. The high-order bits that fall outside the memory bounds of the target data type are permanently discarded.
  3. The vacated low-order bits on the right are padded with zeros.
int value = 3;         // Binary: ... 0000 0011
value <<= 2;           // Shift left by 2
// value is now 12     // Binary: ... 0000 1100

Type Support and Shift Count Masking

The <<= operator is supported for all integral numeric types: sbyte, byte, short, ushort, char, int, uint, long, ulong, nint, and nuint. To prevent undefined behavior from excessively large shift counts, C# applies a bitwise mask to the right operand (y). The mask depends on the bit-width of the left operand after numeric promotion has been applied:
  • 32-bit types and smaller (int, uint, short, ushort, byte, sbyte, char): Because types smaller than 32 bits undergo numeric promotion to int prior to shifting, the shift count is always determined by the lowest 5 bits of the right operand (y & 0x1F). The actual shift count is strictly bounded between 0 and 31.
  • 64-bit types (long, ulong): The shift count is determined by the lowest 6 bits of the right operand (y & 0x3F). The actual shift count is strictly bounded between 0 and 63.
  • Native-sized types (nint, nuint): The shift count mask is 0x1F on 32-bit architectures and 0x3F on 64-bit architectures.
int x = 1;
x <<= 32; 
// 32 in binary is 100000. The lowest 5 bits are 00000 (0).
// Therefore, x is shifted by 0 bits. The result remains 1.

Overflow and Sign Bits

For 32-bit and 64-bit integers (int, uint, long, ulong), the <<= operator operates strictly at the bit level and ignores numeric overflow. It will never throw an OverflowException for these types, regardless of whether the operation occurs within a checked or unchecked context. However, for types smaller than int (such as byte or short), the operation includes an implicit narrowing cast back to the original type after the shift is performed on the promoted int value. If this assignment occurs within a checked context and the shifted value exceeds the bounds of the target type, an OverflowException is thrown.
checked
{
    byte b = 128;
    b <<= 1; // Throws OverflowException because 256 exceeds byte.MaxValue
}
For signed integers, shifting a 1 into the most significant bit (the sign bit) will change the numeric value from positive to negative, as the bit pattern is reinterpreted under two’s complement representation.
int x = 1073741824;    // Binary: 0100 0000 0000 0000 0000 0000 0000 0000
x <<= 1;               // Shifts the 1 into the sign bit
// x is now -2147483648   (Binary: 1000 0000 0000 0000 0000 0000 0000 0000)
Master C# with Deep Grasping Methodology!Learn More