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 built-in primitive data type in Go representing a signed 16-bit integer. It allocates exactly 2 bytes of memory and encodes values using two’s complement binary representation.

Technical Specifications

  • Size: 16 bits (2 bytes)
  • Minimum Value: -32,768 (215-2^{15})
  • Maximum Value: 32,767 (21512^{15}-1)
  • Zero Value: 0
The math package provides exported constants for the boundary values of int16:
import "math"

const min int16 = math.MinInt16 // -32768
const max int16 = math.MaxInt16 // 32767

Declaration and Initialization

Variables of type int16 can be declared using standard Go variable declaration syntax. If no value is assigned, it defaults to its zero value.
// Explicit declaration with initialization
var x int16 = 15000

// Declaration relying on the zero value (0)
var y int16

// Short variable declaration with explicit type conversion
z := int16(-1024)

Strict Typing and Conversion

Go’s type system is strictly enforced. An int16 is a distinct type from int, int8, int32, int64, and uint16. Implicit type promotion or demotion is not supported. You must perform explicit type conversions when performing operations between int16 and other numeric types.
var a int16 = 500
var b int32 = 1000

// Invalid: mismatched types int16 and int32
// result := a + b 

// Valid: explicit conversion to a common type
result := int32(a) + b 

Overflow and Wraparound Behavior

Because int16 has a fixed memory footprint, operations that exceed its maximum or minimum bounds result in an integer overflow. How Go handles this depends on whether the overflow occurs at runtime or compile time. At runtime, Go handles overflow via silent wraparound based on two’s complement arithmetic. Incrementing past the maximum value wraps around to the minimum value, and decrementing below the minimum wraps to the maximum.
package main

import "fmt"

func main() {
	var maxVal int16 = 32767 // math.MaxInt16
	
	// Incrementing past the maximum value wraps around to the minimum value
	maxVal++ 
	fmt.Println(maxVal) // Output: -32768

	var minVal int16 = -32768 // math.MinInt16
	
	// Decrementing below the minimum value wraps around to the maximum value
	minVal--
	fmt.Println(minVal) // Output: 32767
}
At compile time, Go strictly catches overflows in constant expressions. If an operation on constants exceeds the int16 bounds, the compiler throws an error rather than silently wrapping around.
package main

// Valid: within bounds
const a int16 = 32767 

// Invalid: constant 32768 overflows int16
// const b int16 = 32767 + 1 
Master Go with Deep Grasping Methodology!Learn More