> ## 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 `<<=` (bitwise left shift assignment) operator shifts the binary representation of its left operand to the left by the number of bit positions specified by its right operand, and assigns the resulting value back to the left operand.

```cpp theme={"dark"}
lhs <<= rhs;
```

Semantically, `lhs <<= rhs` is equivalent to `lhs = static_cast<T>(lhs << rhs)` (where `T` is the original type of `lhs`), with the strict guarantee that the expression `lhs` is evaluated exactly once. The expression evaluates to an lvalue reference to the left operand (`lhs`), retaining its original, unpromoted type.

## Operand Requirements

The left operand (`lhs`) must be a modifiable lvalue of an integral type. The right operand (`rhs`) must be of an integral or unscoped enumeration type.

Prior to the shift, the compiler performs standard integral promotions on both operands. The intermediate shift operation is performed using the promoted type of the left operand, but the final result is converted back to the original, unpromoted type of `lhs` upon assignment. An unscoped enumeration cannot be used as the left operand because the promoted integer result of the shift cannot be implicitly assigned back to an enumeration type.

## Bit-Level Mechanics

1. **Shift Direction:** The bits of `lhs` are moved to the left by `rhs` positions.
2. **Zero Extension:** The vacated bit positions on the right (least significant bits) are filled with zeros.
3. **Truncation:** Because the result is assigned back to `lhs`, bits that are shifted beyond the most significant bit (MSB) of the *original, unpromoted* type of `lhs` are discarded during the assignment conversion.

```cpp theme={"dark"}
#include <cstdint>

uint8_t val = 0b00001101; // Decimal 13
val <<= 2;                
// val is now 0b00110100 (Decimal 52)
```

## Undefined Behavior (UB)

The `<<=` operator will invoke undefined behavior under the following conditions:

* The right operand (`rhs`) is strictly negative.
* The right operand (`rhs`) is greater than or equal to the total number of bits in the *promoted* type of the left operand (`lhs`).

## C++20 Standard Changes

The behavior of `<<=` on signed integers changed significantly in C++20 due to the mandated adoption of two's complement representation:

* **Pre-C++20:** Left-shifting a negative signed integer, or left-shifting a positive signed integer such that the shifted value cannot be represented in the result type (overflow), resulted in undefined behavior.
* **C++20 and later:** Left-shifting signed integers is fully well-defined. The operation simply discards the bits shifted past the MSB and zero-fills from the right, behaving identically to the corresponding unsigned integer type. Mathematical overflow no longer triggers undefined behavior for this operator.

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