uint16
func uint16[T: Number](x: T): uint16uint16 converts x to uint16 exactly. It panics if x is out of range, has a fraction, or is NaN or infinite; uint16(Math.trunc(x)) truncates. x may also be a named primitive over a number.
Details
uint16holds the whole numbers from0to65535.- The conversion never rounds or wraps.
- Arithmetic on two
uint16values gives auint16, and panics when the result leaves the range, below zero included.
Basic examples
Convert an int or a whole float64:
uint16(8080) // 8080
uint16(65535.0) // 65535
uint16(0) // 0Two uint16 values add to a uint16:
let a = uint16(60000)
let b = uint16(5535)
a + b // 65535Scope
Every numeric type converts, the sized ones included:
uint16(byte(200)) // 200
uint16(int64(65535)) // 65535
uint16(float32(12.0)) // 12
uint16(int8(127)) // 127
uint16(uint16(5)) // 5uint16 is a function value, so it converts every element of an array:
[80, 443, 8080].map(uint16) // [80, 443, 8080]It unwraps a named primitive type over a number:
type Port = uint16
let https = Port(uint16(443))
https // Port(443)
uint16(https) // 443In a pipe, the value is the argument:
("3000".toInt() ?? 0) | uint16() // 3000Applications
Split a uint16 into its two bytes, high byte first:
func toBytes(n: uint16): Bytes {
let hi = byte(Bits.shiftRight(n, 8))
let lo = byte(Bits.and(n, uint16(0xff)))
Bytes.fromArray([hi, lo])
}
toBytes(uint16(0x1234)) // Bytes(12 34)Pack a color into 16 bits as RGB565: five bits of red, six of green, five of blue:
func rgb565(r: int, g: int, b: int): uint16 {
let r5 = Bits.shiftLeft(uint16(r / 8), 11)
let g6 = Bits.shiftLeft(uint16(g / 4), 5)
Bits.or(Bits.or(r5, g6), uint16(b / 8))
}
rgb565(255, 0, 0) // 63488
rgb565(255, 255, 255) // 65535Check a port number read as text before converting it, since uint16 panics on a value out of range:
func parsePort(s: string): uint16 | null {
let n = s.toInt() ?? -1
if n < 1 || n > 65535 {
return null
}
uint16(n)
}
parsePort("8080") // 8080
parsePort("70000") // null
parsePort("http") // nullProperties and relations
Every int in range survives the round trip through uint16:
for n in [0, 1, 255, 256, 65535] {
assert(int(uint16(n)) == n)
}Bits.wrapAdd and Bits.wrapSub wrap around where + and - panic:
Bits.wrapAdd(uint16(65535), uint16(1)) // 0
Bits.wrapSub(uint16(0), uint16(1)) // 65535Possible issues
A value outside the range panics; it does not wrap:
uint16(65536) // panic: 65536 out of range for uint16
uint16(-1) // panic: -1 out of range for uint16A fraction panics instead of truncating:
uint16(2.5) // panic: 2.5 has a fraction, so it does not convert to uint16Round explicitly first; Math.trunc rounds toward zero:
uint16(Math.trunc(2.7)) // 2
uint16(Math.round(2.5)) // 3NaN and the infinities panic:
uint16(Math.NaN) // panic: NaN does not convert to uint16
uint16(-Math.Infinity) // panic: -Infinity does not convert to uint16An integer literal is an int, not a uint16:
let port: uint16 = 80 // error: cannot use int where uint16 is expectedConvert the literal:
let port: uint16 = uint16(80)
port // 80A uint16 and an int do not mix in arithmetic or comparisons:
let a = uint16(1)
println(a < 2) // error: < needs both operands of one type, got uint16 and intConvert the other operand:
let a = uint16(1)
a < uint16(2) // trueSubtraction below zero panics, as does any result above 65535:
uint16(0) - uint16(1) // panic: integer overflow
uint16(300) * uint16(300) // panic: integer overflow
uint16(60000) + uint16(10000) // panic: integer overflowWiden to int before a computation that may leave the range:
int(uint16(300)) * int(uint16(300)) // 90000See also
uint8 · uint32 · uint64 · int16 · int · Math.trunc · Math.round · Bits.wrapAdd · Bits.wrapSub · Bits.shiftLeft · Bits.shiftRight · Arithmetic operators
