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 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
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:
- The bits of the left operand are shifted to the left.
- The high-order bits that fall outside the memory bounds of the target data type are permanently discarded.
- The vacated low-order bits on the right are padded with zeros.
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 tointprior 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 is0x1Fon 32-bit architectures and0x3Fon 64-bit architectures.
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.
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.
Master C# with Deep Grasping Methodology!Learn More





