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

# Rust i16

`i16` is a primitive data type in Rust representing a 16-bit signed integer. It occupies exactly 2 bytes of memory and encodes values using two's complement binary representation, allowing it to store both positive and negative whole numbers.

## Technical Specifications

* **Memory Size:** 16 bits (2 bytes)
* **Representation:** Two's complement (the most significant bit acts as the sign bit)
* **Minimum Value (`i16::MIN`):** -32,768 ($-2^{15}$)
* **Maximum Value (`i16::MAX`):** 32,767 ($2^{15} - 1$)

## Syntax and Initialization

Rust provides multiple ways to instantiate an `i16` value, including explicit type annotation, literal suffixes, and various base representations.

```rust theme={"dark"}
// Explicit type annotation
let explicit: i16 = 15000;

// Literal suffix type inference
let suffixed = -250i16;

// Hexadecimal representation
let hex: i16 = 0x3E8; // 1000 in decimal

// Binary representation with visual separators
let bin: i16 = 0b0011_1110_1000; // 1000 in decimal
```

## Overflow Behavior

By default, Rust handles integer overflow differently depending on the compilation profile:

* **Debug mode:** Integer operations that exceed the `i16` bounds will cause the program to panic.
* **Release mode:** Integer operations perform two's complement wrapping (e.g., `i16::MAX + 1` wraps to `i16::MIN`).

To explicitly define overflow behavior regardless of the compilation profile, `i16` implements specific standard library methods:

```rust theme={"dark"}
let max = i16::MAX; // 32767

// Checked operations return an Option<i16>
// Returns None if an overflow occurs
let checked = max.checked_add(1); 

// Wrapping operations wrap around the boundary
// Returns -32768
let wrapped = max.wrapping_add(1); 

// Saturating operations clamp to the boundary limits
// Returns 32767
let saturated = max.saturating_add(1); 

// Overflowing operations return a tuple: (result, boolean_flag)
// Returns (-32768, true)
let (overflowed_val, did_overflow) = max.overflowing_add(1);
```

## Type Casting

Conversion between `i16` and other numeric primitives is performed using the `as` keyword. Casting from a larger integer type to `i16` results in truncation of the most significant bits, which can alter both the magnitude and the sign of the value.

```rust theme={"dark"}
let large_val: i32 = 65535;

// Truncates the upper 16 bits of the i32
// 65535 (0x0000FFFF) becomes -1 (0xFFFF) in two's complement i16
let truncated: i16 = large_val as i16;

let small_val: i8 = -50;

// Sign-extends the i8 to fit the i16 memory space
let extended: i16 = small_val as i16; 
```

## Bitwise Operations

As a primitive integer, `i16` fully supports bitwise operations. These operate directly on the 16-bit binary representation.

```rust theme={"dark"}
let a: i16 = 0b0000_0000_0000_1100; // 12
let b: i16 = 0b0000_0000_0000_1010; // 10

let bit_and = a & b;  // 0b1000 (8)
let bit_or  = a | b;  // 0b1110 (14)
let bit_xor = a ^ b;  // 0b0110 (6)
let bit_not = !a;     // 0b1111_1111_1111_0011 (-13)

// Bit shifting
let shift_left = a << 2;  // 48
let shift_right = a >> 1; // 6 (Arithmetic shift right, preserves sign bit)
```

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