Skip to main content

Try it Live

Run Uint examples in the interactive playground

Uint16

A branded type for 16-bit unsigned integers (range: 0-65535) represented as JavaScript numbers with compile-time type safety.

Overview

Uint16 provides type-safe operations on 16-bit unsigned integers with:
  • Compile-time type branding for type safety
  • Runtime validation and overflow/underflow checking
  • Zero runtime overhead (just a number internally)
  • Full arithmetic, bitwise, and comparison operations

Type Definition

Range

  • Minimum: 0
  • Maximum: 65535
  • Size: 16 bits

Constants

Construction

from

Create from number or string:

fromNumber

Create from number:

fromBigint

Create from bigint:

fromHex

Create from hex string:

fromBytes

Create from Uint8Array (2 bytes, big-endian):

Conversion

toNumber

toBigint

toHex

toBytes

toString

Arithmetic

All arithmetic operations validate that results stay within valid range.

plus

minus

times

dividedBy

modulo

Comparison

equals

lessThan

greaterThan

isZero

minimum

maximum

Bitwise Operations

bitwiseAnd

bitwiseOr

bitwiseXor

bitwiseNot

shiftLeft

shiftRight

Bit Analysis

bitLength

Get number of bits needed to represent value:

leadingZeros

Count leading zero bits:

popCount

Count set bits:

Validation

isValid

Use Cases

  • Network port numbers
  • Protocol buffers and binary formats
  • File format headers and metadata
  • Unicode character codes (BMP)
  • Audio sample values
  • Sensor readings and measurements
  • Memory addresses (in 16-bit systems)
  • Checksums and hash values

Error Handling

All operations throw on invalid inputs:
  • Non-integer values
  • Negative values
  • Values exceeding 65535
  • Arithmetic overflow/underflow
  • Division by zero

Example