A class that represents a signed or unsigned integer of a specific bit size.

Constructors

  • Creates a new CheckedBigInt. with custom boundaries

    Parameters

    • value: bigint

      initial value

    • bounds: Bounds

      the set boundaries for the instance

    Returns CheckedBigInt

    const a = new CheckedBigInt(1n, {min: 1n, max: 10n})
    
  • Creates a new CheckedBigInt. with a specific bit size and signedness

    Parameters

    • value: bigint

      initial value

    • bits: number

      the bit size of the integer

    • signed: boolean

      whether the integer is signed or not

    Returns CheckedBigInt

    const a = new CheckedBigInt(1n /* value */, 8 /*bits*/, false/*unsigned*/)
    

Properties

boundaries: Bounds
value: bigint

Methods

  • Add another integer to this integer, checking for overflow.

    Parameters

    • other: bigint

      The integer to add.

    Returns CheckedBigInt

    A new CheckedBigInt with the result of the addition.

    RangeError If the result of the addition would exceed the bit size.

    const a = i8(1n).add(1n) // 2
    const b = i8(127n).add(1n) // throws RangeError
  • Divide this integer by another integer, checking for overflow.

    Parameters

    • other: bigint

      The integer to divide by.

    Returns CheckedBigInt

    A new CheckedBigInt with the result of the division.

    RangeError If the result of the division would exceed the bit size.

    RangeError If the divisor is zero.

  • Check equality with another value.

    Parameters

    • other: bigint

      The value to compare with.

    Returns boolean

    true if equal, false otherwise.

  • Check if this value is greater than another.

    Parameters

    • other: bigint

      The value to compare with.

    Returns boolean

    true if greater than, false otherwise.

  • Check if this value is greater than or equal to another.

    Parameters

    • other: bigint

      The value to compare with.

    Returns boolean

    true if greater than or equal, false otherwise.

  • Check if this value is less than another.

    Parameters

    • other: bigint

      The value to compare with.

    Returns boolean

    true if less than, false otherwise.

  • Check if this value is less than or equal to another.

    Parameters

    • other: bigint

      The value to compare with.

    Returns boolean

    true if less than or equal, false otherwise.

  • Modulo operation (always returns non-negative result for positive divisor). Unlike rem(), mod() follows mathematical modulo semantics where the result has the same sign as the divisor.

    Parameters

    • other: bigint

      The integer to divide by.

    Returns CheckedBigInt

    A new CheckedBigInt with the modulo result.

    RangeError If the divisor is zero.

    i8(-5n).mod(3n)  // 1n (mathematical modulo)
    i8(-5n).rem(3n) // -2n (remainder)
  • Multiply this integer by another integer, checking for overflow.

    Parameters

    • other: bigint

      The integer to multiply by.

    Returns CheckedBigInt

    A new CheckedBigInt with the result of the multiplication.

    RangeError If the result of the multiplication would exceed the bit size.

  • Check inequality with another value.

    Parameters

    • other: bigint

      The value to compare with.

    Returns boolean

    true if not equal, false otherwise.

  • Power this integer to another integer, checking for overflow.

    Parameters

    • exponent: bigint

      The integer to power by.

    Returns CheckedBigInt

    A new CheckedBigInt with the result of the power.

    RangeError If the exponent is negative.

    RangeError If the result of the power would exceed the bit size.

  • Remainder of the division of this integer by another integer, checking for overflow.

    Parameters

    • other: bigint

      The integer to divide by.

    Returns CheckedBigInt

    A new CheckedBigInt with the result of the remainder.

    RangeError If the result of the remainder would exceed the bit size.

    RangeError If the divisor is zero.

  • Bitwise shift left this integer by another integer, checking for overflow.

    Parameters

    • bits: number | bigint

      The integer to shift by.

    Returns CheckedBigInt

    A new CheckedBigInt with the result of the shift.

    RangeError If the number of bits is negative.

    RangeError If the resulting number would exceed the bit size.

  • Bitwise shift right this integer by another integer, checking for overflow.

    Parameters

    • bits: number | bigint

      The integer to shift by.

    Returns CheckedBigInt

    A new CheckedBigInt with the result of the shift.

    RangeError If the number of bits is negative.

    RangeError If bits is greater than the bit size.

  • Subtract another integer from this integer, checking for overflow.

    Parameters

    • other: bigint

      The integer to subtract.

    Returns CheckedBigInt

    A new CheckedBigInt with the result of the subtraction.

    RangeError If the result of the subtraction would exceed the bit size.

  • Returns the value for JSON serialization. Note: BigInt values are converted to strings in JSON.

    Returns string

    The string representation of the value.

  • Returns the string representation of the value.

    Parameters

    • Optionalradix: number

      Optional radix for the string representation (default: 10).

    Returns string

    The string representation of the value.

  • Returns the primitive value (for use with valueOf).

    Returns bigint

    The bigint value.