Skip to main content

Try it Live

Run Int examples in the interactive playground

Type Definition

Branded bigint representing signed 128-bit integer (-2^127 to 2^127-1). Supports two’s complement encoding for hex/bytes operations.

Range

  • MIN: -2^127 = -170141183460469231731687303715884105728
  • MAX: 2^127-1 = 170141183460469231731687303715884105727
  • Size: 16 bytes (128 bits)

Quick Start

API Methods

Constructors

  • from(value) - From bigint, number, or string
  • fromBigInt(value) - From bigint with validation
  • fromNumber(value) - From number (throws if out of range)
  • fromHex(hex) - Parse hex (two’s complement)
  • fromBytes(bytes) - Parse bytes (two’s complement, big-endian)

Conversions

  • toHex(value) - To hex (two’s complement)
  • toBytes(value) - To bytes (16 bytes, two’s complement, big-endian)
  • toBigInt(value) - To bigint
  • toNumber(value) - To number (throws if unsafe)
  • toString(value) - To decimal string

Arithmetic

  • plus(a, b) - Add with wrapping
  • minus(a, b) - Subtract with wrapping
  • times(a, b) - Multiply with wrapping
  • dividedBy(a, b) - Divide (truncate toward zero)
  • modulo(a, b) - Modulo (sign follows dividend)
  • abs(value) - Absolute value (throws on MIN)
  • negate(value) - Negate with wrapping

Comparison

  • equals(a, b) - Equality
  • lessThan(a, b) - Signed less than
  • greaterThan(a, b) - Signed greater than
  • isZero(value) - Check if zero
  • isNegative(value) - Check if negative
  • isPositive(value) - Check if positive
  • sign(value) - Sign indicator (-1, 0, 1)
  • minimum(a, b) - Minimum value
  • maximum(a, b) - Maximum value

Bitwise

  • bitwiseAnd(a, b) - Bitwise AND
  • bitwiseOr(a, b) - Bitwise OR
  • bitwiseXor(a, b) - Bitwise XOR
  • bitwiseNot(value) - Bitwise NOT
  • shiftLeft(value, shift) - Left shift
  • shiftRight(value, shift) - Arithmetic right shift (sign-preserving)

Utilities

  • bitLength(value) - Significant bit count
  • leadingZeros(value) - Leading zero count
  • popCount(value) - Set bit count
  • isValid(value) - Range validation

Two’s Complement

Int128 uses two’s complement for hex/bytes encoding:

Division Semantics

Division truncates toward zero (not floor):

Arithmetic Right Shift

shiftRight preserves sign (arithmetic shift):

Edge Cases

Constants