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.

Int16 is a value type in Swift representing a signed 16-bit (2-byte) integer. It stores whole numbers using two’s complement binary representation, allocating 1 bit for the sign and 15 bits for the magnitude.

Value Range

Because it is a 16-bit signed integer, Int16 has a strictly defined mathematical range:
  • Minimum: 215-2^{15} or -32,768
  • Maximum: 21512^{15} - 1 or 32,767
These boundaries can be accessed programmatically via static properties:
let lowestValue = Int16.min  // -32768
let highestValue = Int16.max // 32767

Initialization and Syntax

Swift infers integer literals as the architecture-dependent Int type by default. To create an Int16, you must explicitly declare the type annotation or use an initializer.
// Type annotation
let explicitSixteen: Int16 = 15000

// Initialization via type conversion
let defaultInt = 42
let convertedSixteen = Int16(defaultInt)

// Hexadecimal, octal, and binary literals
let hexSixteen: Int16 = 0x7FFF // 32767
let binSixteen: Int16 = 0b0000_1111_1111_1111 // 4095

Memory Layout

The memory footprint of Int16 is guaranteed to be exactly 2 bytes across all platforms, regardless of whether the underlying architecture is 32-bit or 64-bit.
let byteSize = MemoryLayout<Int16>.size       // 2 bytes
let bitWidth = Int16.bitWidth                 // 16 bits
let alignment = MemoryLayout<Int16>.alignment // 2 bytes

Type Safety and Conversion

Swift enforces strict type safety and does not perform implicit type conversions between different integer sizes. An Int16 cannot be directly added to an Int8, Int32, Int64, or Int without explicit casting.
let a: Int16 = 1000
let b: Int8 = 10

// let result = a + b // Compile-time error: binary operator '+' cannot be applied to operands of type 'Int16' and 'Int8'
let result = a + Int16(b) // Valid: 'b' is explicitly promoted to Int16

Overflow Behavior

By default, Swift arithmetic operators (+, -, *) trap and cause a runtime crash if an operation results in a value outside the Int16 range. To perform modulo arithmetic that truncates the result instead of crashing, you must use Swift’s overflow operators (&+, &-, &*).
var maxVal = Int16.max

// maxVal += 1 // Runtime crash: arithmetic overflow

// Two's complement wrap-around
let wrappedVal = maxVal &+ 1 // Evaluates to -32768 (Int16.min)

Protocol Conformances

As a fundamental numeric type, Int16 conforms to several standard library protocols that dictate its behavior:
  • FixedWidthInteger: Guarantees a static bit width and provides bitwise operations.
  • SignedInteger: Indicates the type can represent both positive and negative values.
  • Hashable & Equatable: Allows Int16 to be used as dictionary keys and compared for equality.
  • Codable: Supports native serialization and deserialization.
Master Swift with Deep Grasping Methodology!Learn More