README

#int16

This package provides a fixed-width 16-bit signed integer type.

#Range and Constants

The Int16 type represents values from -32768 to 32767 (inclusive). The package provides these boundary values as constants:

///|
test "int16 range" {
inspect(@int16.MIN_VALUE, content="-32768")
inspect(@int16.MAX_VALUE, content="32767")
}

#Arithmetic Operations

The Int16 type supports standard arithmetic operations:

///|
test "int16 arithmetic" {
let a : Int16 = 100
let b : Int16 = 50

// Basic arithmetic
inspect(a + b, content="150")
inspect(a - b, content="50")
inspect(a * b, content="5000")
inspect(a / b, content="2")

// Overflow behavior
let max = @int16.MAX_VALUE
let min = @int16.MIN_VALUE
inspect(max + 1, content="-32768") // Wraps around to MIN_VALUE
inspect(min - 1, content="32767") // Wraps around to MAX_VALUE
}

#Bitwise Operations

Int16 supports standard bitwise operations:

///|
test "int16 bitwise" {
let a : Int16 = 0b1100
let b : Int16 = 0b1010

// Bitwise AND, OR, XOR
inspect(a & b, content="8") // 0b1000
inspect(a | b, content="14") // 0b1110
inspect(a ^ b, content="6") // 0b0110

// Bit shifts
let x : Int16 = 8
inspect(x << 1, content="16") // Left shift
inspect(x >> 1, content="4") // Right shift
}

#Comparison Operations

Int16 implements the Compare trait for total ordering:

///|
test "int16 comparison" {
let a : Int16 = 100
let b : Int16 = 50
let c : Int16 = 100

// Equality
inspect(a == b, content="false")
inspect(a == c, content="true")

// Ordering
inspect(a > b, content="true")
inspect(b < c, content="true")

// Compare function returns -1, 0, or 1
inspect(a.compare(b), content="1")
inspect(b.compare(c), content="-1")
inspect(a.compare(c), content="0")
}

#Default Value

Int16 implements the Default trait, with 0 as its default value:

///|
test "int16 default" {
let x = (Default::default() : Int16)
inspect(x, content="0")
}

#Type Coercion and Conversion

Integer literals can be coerced to Int16 when the type is explicitly specified:

///|
test "int16 coercion" {
let a : Int16 = 42 // Coercion from integer literal
let b : Int16 = 0xFF // Hexadecimal literal
let c : Int16 = 0b1111 // Binary literal
inspect(a, content="42")
inspect(b, content="255")
inspect(c, content="15")
}

Int16

Note

Int16 is a built-in type. The documentation may not be complete here. You can find all methods and implementations in the core/builtin and core/int16 package.

#
Int16::abs

fn Int16::abs(self : Int16) -> Int16

Return absolute value.

#
Int16::add

fn Int16::add(self : Int16, that : Int16) -> Int16

#
Int16::compare

fn Int16::compare(self : Int16, that : Int16) -> Int

#
Int16::div

fn Int16::div(self : Int16, that : Int16) -> Int16

#
Int16::equal

fn Int16::equal(self : Int16, that : Int16) -> Bool

#
Int16::from_byte

fn Int16::from_byte(self : Byte) -> Int16

Converts a Byte value to a 16-bit signed integer (Int16).

This function extends the byte value (0-255) to a 16-bit signed integer. Values from 0-127 map to themselves. Values from 128-255 are treated as unsigned and map to 128-255 in Int16.

Parameters:

  • self : The byte value to be converted to an Int16.

Returns an Int16 value where the byte is zero-extended to 16 bits and then interpreted as a signed integer.

Example:

test {
let b = b'\xFF'
inspect(Int16::from_byte(b), content="255") // Sign is preserved
let p = b'\x7F'
inspect(Int16::from_byte(p), content="127")
}

#
Int16::from_int

fn Int16::from_int(self : Int) -> Int16

Converts an Int value to a 16-bit signed integer (Int16).

This function performs a truncating conversion from a 32-bit signed integer to a 16-bit signed integer. Values outside the valid range for Int16 (-32768 to 32767) will be truncated.

Parameters:

  • self : The Int value to be converted.

Returns an Int16 representing the lower 16 bits of the input value, interpreted as a signed integer.

Example:

test {
let n = Int16::from_int(42)
inspect(n, content="42")
let neg = Int16::from_int(-42)
inspect(neg, content="-42")
}

#
Int16::from_int64

fn Int16::from_int64(self : Int64) -> Int16

Converts an Int64 value to a 16-bit signed integer (Int16).

This function performs a truncating conversion from a 64-bit signed integer to a 16-bit signed integer, taking only the lower 16 bits of the input. Values outside the valid range for Int16 will be truncated to fit.

Parameters:

  • self : The Int64 value to be converted.

Returns an Int16 representing the lower 16 bits of the input value.

Example:

test {
let big = 100000L
inspect(Int16::from_int64(big), content="-31072") // 100000 doesn't fit in Int16, gets truncated
let small = 42L
inspect(Int16::from_int64(small), content="42") // 42 fits in Int16, remains unchanged
}
Create from int64.

#
Int16::hash

fn Int16::hash(self : Int16) -> Int

#
Int16::land

fn Int16::land(self : Int16, that : Int16) -> Int16

#
Int16::lnot

fn Int16::lnot(self : Int16) -> Int16

Performs a bitwise NOT operation on an Int16 value, flipping every bit within its 16-bit width.

Parameters:

  • self : The Int16 value to apply the bitwise NOT operation on.

Returns the result of the bitwise NOT operation as an Int16.

Example:

test {
inspect((0 : Int16).lnot(), content="-1")
inspect((-1 : Int16).lnot(), content="0")
}

#
Int16::lor

fn Int16::lor(self : Int16, that : Int16) -> Int16

#
Int16::lxor

fn Int16::lxor(self : Int16, that : Int16) -> Int16

#
Int16::mod

fn Int16::mod(self : Int16, that : Int16) -> Int16

#
Int16::mul

fn Int16::mul(self : Int16, that : Int16) -> Int16

#
Int16::neg

fn Int16::neg(self : Int16) -> Int16

#
Int16::reinterpret_as_uint16

fn Int16::reinterpret_as_uint16(self : Int16) -> UInt16

reinterpret as an unsigned integer with binary complement

#
Int16::reinterpret_from_uint16

fn Int16::reinterpret_from_uint16(self : UInt16) -> Int16

reinterpret from an unsigned integer with binary complement

#
Int16::shl

fn Int16::shl(self : Int16, that : Int) -> Int16

#
Int16::shr

fn Int16::shr(self : Int16, that : Int) -> Int16

#
Int16::sub

fn Int16::sub(self : Int16, that : Int16) -> Int16

#
Int16::to_byte

fn Int16::to_byte(self : Int16) -> Byte

Converts a 16-bit signed integer to a byte by truncating its value to fit within the byte range (0 to 255). Only the least significant 8 bits of the integer are retained.

Parameters:

  • value : The 16-bit signed integer to be converted to a byte.

Returns a byte containing the least significant 8 bits of the input value.

Example:

test {
let x : Int16 = 258 // In binary: 0000_0001_0000_0010
inspect(x.to_byte(), content="b'\\x02'") // Only keeps 0000_0010
}

#
Int16::to_int

fn Int16::to_int(self : Int16) -> Int

Converts a 16-bit signed integer to a 32-bit signed integer by sign extension.

Parameters:

  • value : The 16-bit signed integer to be converted.

Returns a 32-bit signed integer that has the same value as the input.

Example:

test {
let n = (42 : Int16)
inspect(n.to_int(), content="42")
let neg = (-42 : Int16)
inspect(neg.to_int(), content="-42")
}

#
Int16::to_int64

fn Int16::to_int64(self : Int16) -> Int64

Convert to int64.

#
Int16::to_string

fn Int16::to_string(self : Int16, radix? : Int) -> String

Convert to string.
impl Add for Int16
impl BitAnd for Int16
impl BitOr for Int16
impl BitXOr for Int16
impl Compare for Int16
impl Default for Int16
impl Div for Int16
impl Eq for Int16
impl Hash for Int16
impl Mod for Int16
impl Mul for Int16
impl Neg for Int16
impl Shl for Int16
impl Show for Int16
impl Shr for Int16
impl Sub for Int16
impl ToJson for Int16

#
MAX_VALUE

let MAX_VALUE : Int16

Max value constant for this type.

#
MIN_VALUE

let MIN_VALUE : Int16

Min value constant for this type.

#
max_value

#deprecated("Use `MAX_VALUE` instead")
let max_value : Int16

Max value constant for this type.

#
min_value

#deprecated("Use `MIN_VALUE` instead")
let min_value : Int16

Min value constant for this type.