> ## 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.

# C# Left Shift Assignment

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

```csharp theme={"dark"}
x <<= y;
```

This expression is semantically equivalent to:

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
