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.

Int8 is a value type in Swift that represents an 8-bit signed integer. It occupies exactly one byte of memory and stores values using two’s complement binary representation, allowing it to represent both positive and negative whole numbers within a strictly defined 8-bit boundary.

Value Range and Memory Footprint

Because it is constrained to 8 bits, Int8 has a fixed mathematical range of 27-2^7 to 2712^7 - 1. Swift provides static properties to access these boundaries programmatically.
let lowestValue: Int8 = Int8.min   // -128
let highestValue: Int8 = Int8.max  // 127
let allocatedBits: Int = Int8.bitWidth // 8

Type Safety and Instantiation

Swift enforces strict type safety and does not perform implicit type coercion between different integer sizes. An Int8 cannot be directly assigned to or operated on with an Int, Int16, or UInt8 without explicit initialization.
let defaultInt: Int = 100

// Implicit conversion fails at compile-time:
// let invalidByte: Int8 = defaultInt 

// Explicit initialization is required:
let validByte: Int8 = Int8(defaultInt)
If an explicit initialization is attempted with a value outside the [-128, 127] range, Swift will trigger a runtime crash. To handle potential out-of-bounds conversions safely, use the exactly parameter, which returns an optional:
let largeInt: Int = 200
let safeByte: Int8? = Int8(exactly: largeInt) // Returns nil

Arithmetic and Overflow Behavior

Due to its narrow range, Int8 is highly susceptible to arithmetic overflow. Standard arithmetic operators (+, -, *) check for overflow and will trigger a runtime trap (EXC_BAD_INSTRUCTION) if the result exceeds 8 bits. To intentionally allow two’s complement wrap-around behavior, Swift requires the use of explicit overflow operators (&+, &-, &*).
let maxBoundary: Int8 = 127

// Standard addition causes a runtime crash:
// let crash = maxBoundary + 1 

// Overflow addition wraps around to the minimum value:
let wrapped: Int8 = maxBoundary &+ 1 // -128

Bitwise Operations

As a single-byte structure, Int8 supports all standard Swift bitwise operators (~, &, |, ^, <<, >>). When performing bitwise shifts on an Int8, the sign bit (the most significant bit) is preserved during right shifts (arithmetic shift).
let bitPattern: Int8 = 0b0000_1111 // 15 in decimal
let shiftedLeft: Int8 = bitPattern << 3 // 0b0111_1000 (120 in decimal)

let negativeBits: Int8 = -128 // 0b1000_0000
let shiftedRight: Int8 = negativeBits >> 1 // 0b1100_0000 (-64 in decimal, sign bit preserved)
Master Swift with Deep Grasping Methodology!Learn More