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.

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 (215-2^{15})
  • Maximum Value (i16::MAX): 32,767 (21512^{15} - 1)

Syntax and Initialization

Rust provides multiple ways to instantiate an i16 value, including explicit type annotation, literal suffixes, and various base representations.
// 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:
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.
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.
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)
Master Rust with Deep Grasping Methodology!Learn More