Class Long

A class representing a 64-bit integer

The internal representation of a long is the two given signed, 32-bit values. We use 32-bit pieces because these are the size of integers on which Javascript performs bit-operations. For operations like addition and multiplication, we split each number into 16 bit pieces, which can easily be multiplied within Javascript's floating-point representation without overflow or change in sign. In the algorithms below, we frequently reduce the negative case to the positive case by negating the input(s) and then post-processing the result. Note that we must ALWAYS check specially whether those values are MIN_VALUE (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as a positive number, it overflows back into a negative). Not handling this case would often result in infinite recursion. Common constant values ZERO, ONE, NEG_ONE, etc. are found as static properties on this class.

Hierarchy (view full)

Constructors

  • Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as signed integers.

    Parameters

    • low: number

      The low (signed) 32 bits of the long

    • Optionalhigh: number

      The high (signed) 32 bits of the long

    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    Returns Long

  • Constructs a 64 bit two's-complement integer, given a bigint representation.

    Parameters

    • value: bigint

      BigInt representation of the long value

    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    Returns Long

  • Constructs a 64 bit two's-complement integer, given a string representation.

    Parameters

    • value: string

      String representation of the long value

    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    Returns Long

Properties

high: number

The high 32 bits as a signed value.

low: number

The low 32 bits as a signed value.

unsigned: boolean

Whether unsigned or not.

MAX_UNSIGNED_VALUE: Long

Maximum unsigned value.

MAX_VALUE: Long

Maximum signed value.

MIN_VALUE: Long

Minimum signed value.

NEG_ONE: Long

Signed negative one.

ONE: Long

Signed one.

TWO_PWR_24: Long
UONE: Long

Unsigned one.

UZERO: Long

Unsigned zero.

ZERO: Long

Signed zero

Accessors

  • get __isLong__(): boolean
  • An indicator used to reliably determine if an object is a Long or not.

    Returns boolean

  • get _bsontype(): "Long"
  • Returns "Long"

Methods

  • Returns the sum of this and the specified Long.

    Parameters

    • addend:
          | string
          | number
          | Timestamp
          | Long

    Returns Long

  • Returns the sum of this and the specified Long.

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns Long

    Sum

  • This is an alias of Long.compare

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns -1 | 0 | 1

  • Compares this Long's value with the specified's.

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns -1 | 0 | 1

    0 if they are the same, 1 if the this is greater and -1 if the given one is greater

  • This is an alias of Long.divide

    Parameters

    • divisor:
          | string
          | number
          | Timestamp
          | Long

    Returns Long

  • Returns this Long divided by the specified. The result is signed if this Long is signed or unsigned if this Long is unsigned.

    Parameters

    • divisor:
          | string
          | number
          | Timestamp
          | Long

    Returns Long

    Quotient

  • This is an alias of Long.equals

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns boolean

  • Tests if this Long's value equals the specified's.

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

      Other value

    Returns boolean

  • This is an alias of Long.isZero

    Returns boolean

  • This is an alias of Long.greaterThanOrEqual

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns boolean

  • Gets the high 32 bits as a signed integer.

    Returns number

  • Gets the high 32 bits as an unsigned integer.

    Returns number

  • Gets the low 32 bits as a signed integer.

    Returns number

  • Gets the low 32 bits as an unsigned integer.

    Returns number

  • Gets the number of bits needed to represent the absolute value of this Long.

    Returns number

  • Tests if this Long's value is greater than the specified's.

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns boolean

  • Tests if this Long's value is greater than or equal the specified's.

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns boolean

  • This is an alias of Long.greaterThan

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns boolean

  • This is an alias of Long.greaterThanOrEqual

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns boolean

  • Prints a human-readable string of BSON value information If invoked manually without node.js.inspect function, this will default to a modified JSON.stringify

    Parameters

    • Optionaldepth: number
    • Optionaloptions: unknown
    • Optionalinspect: InspectFn

    Returns string

  • Tests if this Long's value is even.

    Returns boolean

  • Tests if this Long's value is negative.

    Returns boolean

  • Tests if this Long's value is odd.

    Returns boolean

  • Tests if this Long's value is positive.

    Returns boolean

  • Tests if this Long's value equals zero.

    Returns boolean

  • This is an alias of Long.lessThanOrEqual

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns boolean

  • Tests if this Long's value is less than the specified's.

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns boolean

  • Tests if this Long's value is less than or equal the specified's.

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns boolean

  • This is an alias of Long#lessThan.

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns boolean

  • This is an alias of Long.lessThanOrEqual

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns boolean

  • This is an alias of Long.modulo

    Parameters

    • divisor:
          | string
          | number
          | Timestamp
          | Long

    Returns Long

  • Returns this Long modulo the specified.

    Parameters

    • divisor:
          | string
          | number
          | Timestamp
          | Long

    Returns Long

  • This is an alias of Long.multiply

    Parameters

    • multiplier:
          | string
          | number
          | Timestamp
          | Long

    Returns Long

  • Returns the product of this and the specified Long.

    Parameters

    • multiplier:
          | string
          | number
          | Timestamp
          | Long

      Multiplier

    Returns Long

    Product

  • This is an alias of Long.notEquals

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns boolean

  • This is an alias of Long.negate

    Returns Long

  • Returns the Negation of this Long's value.

    Returns Long

  • This is an alias of Long.notEquals

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns boolean

  • Returns the bitwise NOT of this Long.

    Returns Long

  • Tests if this Long's value differs from the specified's.

    Parameters

    • other:
          | string
          | number
          | Timestamp
          | Long

    Returns boolean

  • Returns the bitwise OR of this Long and the specified.

    Parameters

    • other: string | number | Long

    Returns Long

  • This is an alias of Long.modulo

    Parameters

    • divisor:
          | string
          | number
          | Timestamp
          | Long

    Returns Long

  • Returns this Long with bits shifted to the left by the given amount.

    Parameters

    • numBits: number | Long

      Number of bits

    Returns Long

    Shifted Long

  • Returns this Long with bits arithmetically shifted to the right by the given amount.

    Parameters

    • numBits: number | Long

      Number of bits

    Returns Long

    Shifted Long

  • Returns this Long with bits logically shifted to the right by the given amount.

    Parameters

    • numBits: number | Long

      Number of bits

    Returns Long

    Shifted Long

  • This is an alias of Long.shiftLeft

    Parameters

    • numBits: number | Long

    Returns Long

  • This is an alias of Long.shiftRight

    Parameters

    • numBits: number | Long

    Returns Long

  • This is an alias of Long.subtract

    Parameters

    • subtrahend:
          | string
          | number
          | Timestamp
          | Long

    Returns Long

  • Returns the difference of this and the specified Long.

    Parameters

    • subtrahend:
          | string
          | number
          | Timestamp
          | Long

      Subtrahend

    Returns Long

    Difference

  • Converts the Long to a BigInt (arbitrary precision).

    Returns bigint

  • Converts this Long to its byte representation.

    Parameters

    • Optionalle: boolean

      Whether little or big endian, defaults to big endian

    Returns number[]

    Byte representation

  • Converts this Long to its big endian byte representation.

    Returns number[]

    Big endian byte representation

  • Converts this Long to its little endian byte representation.

    Returns number[]

    Little endian byte representation

  • Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.

    Returns number

  • Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).

    Returns number

  • Converts this Long to signed.

    Returns Long

  • Converts the Long to a string written in the specified radix.

    Parameters

    • Optionalradix: number

      Radix (2-36), defaults to 10

    Returns string

    RangeError If radix is out of range

  • Converts this Long to unsigned.

    Returns Long

  • Returns the bitwise XOR of this Long and the given one.

    Parameters

    • other: string | number | Long

    Returns Long

  • Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.

    Parameters

    • value: bigint

      The number in question

    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    Returns Long

    The corresponding Long value

  • Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is assumed to use 32 bits.

    Parameters

    • lowBits: number

      The low 32 bits

    • highBits: number

      The high 32 bits

    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    Returns Long

    The corresponding Long value

  • Creates a Long from its byte representation.

    Parameters

    • bytes: number[]

      Byte representation

    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    • Optionalle: boolean

      Whether little or big endian, defaults to big endian

    Returns Long

    The corresponding Long value

  • Creates a Long from its big endian byte representation.

    Parameters

    • bytes: number[]

      Big endian byte representation

    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    Returns Long

    The corresponding Long value

  • Creates a Long from its little endian byte representation.

    Parameters

    • bytes: number[]

      Little endian byte representation

    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    Returns Long

    The corresponding Long value

  • Parameters

    • doc: {
          $numberLong: string;
      }
      • $numberLong: string
    • Optionaloptions: EJSONOptions

    Returns number | bigint | Long

  • Returns a Long representing the given 32 bit integer value.

    Parameters

    • value: number

      The 32 bit integer in question

    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    Returns Long

    The corresponding Long value

  • Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.

    Parameters

    • value: number

      The number in question

    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    Returns Long

    The corresponding Long value

  • Returns a signed Long representation of the given string, written using radix 10.

    If the input string is empty, this function will throw a BSONError.

    If input string does not have valid signed 64-bit Long representation, this method will return a coerced value:

    • inputs that overflow 64-bit signed long will be coerced to Long.MAX_VALUE and Long.MIN_VALUE respectively
    • 'NaN' or '+/-Infinity' are coerced to Long.ZERO
    • other invalid characters sequences have variable behavior

    Parameters

    • str: string

      The textual representation of the Long

    Returns Long

    The corresponding Long value

  • Returns a signed Long representation of the given string, written using the provided radix.

    If the input string is empty or a provided radix is not within (2-36), this function will throw a BSONError.

    If input parameters do not have valid signed 64-bit Long representation, this method will return a coerced value:

    • inputs that overflow 64-bit signed long will be coerced to Long.MAX_VALUE and Long.MIN_VALUE respectively
    • if the radix is less than 24, 'NaN' is coerced to Long.ZERO
    • if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO
    • other invalid characters sequences have variable behavior

    Parameters

    • str: string

      The textual representation of the Long

    • Optionalradix: number

      The radix in which the text is written (2-36), defaults to 10

    Returns Long

    The corresponding Long value

  • Returns a Long representation of the given string, written using radix 10.

    If the input string is empty, this function will throw a BSONError.

    If input parameters do not have a valid 64-bit Long representation, this method will return a coerced value:

    • inputs that overflow 64-bit long will be coerced to max or min (if signed) values
    • if the radix is less than 24, 'NaN' is coerced to Long.ZERO
    • if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO
    • other invalid characters sequences have variable behavior

    Parameters

    • str: string

      The textual representation of the Long

    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    Returns Long

    The corresponding Long value

  • Returns a Long representation of the given string, written using the specified radix.

    If the input string is empty or a provided radix is not within (2-36), this function will throw a BSONError.

    If input parameters do not have a valid 64-bit Long representation, this method will return a coerced value:

    • inputs that overflow 64-bit long will be coerced to max or min (if signed) values
    • if the radix is less than 24, 'NaN' is coerced to Long.ZERO
    • if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO
    • other invalid characters sequences have variable behavior

    Parameters

    • str: string

      The textual representation of the Long

    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    • Optionalradix: number

      The radix in which the text is written (2-36), defaults to 10

    Returns Long

    The corresponding Long value

  • Returns a signed Long representation of the given string, written using radix 10. Will throw an error if the given text is not exactly representable as a Long. Throws an error if any of the following conditions are true:

    • the string contains invalid characters for the radix 10
    • the string contains whitespace
    • the value the string represents is too large or too small to be a Long Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero

    Parameters

    • str: string

      The textual representation of the Long

    Returns Long

    The corresponding Long value

  • Returns a Long representation of the given string, written using the radix 10. Will throw an error if the given parameters are not exactly representable as a Long. Throws an error if any of the following conditions are true:

    • the string contains invalid characters for the given radix
    • the string contains whitespace
    • the value the string represents is too large or too small to be a Long Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero

    Parameters

    • str: string

      The textual representation of the Long

    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    Returns Long

    The corresponding Long value

  • Returns a signed Long representation of the given string, written using the specified radix. Will throw an error if the given parameters are not exactly representable as a Long. Throws an error if any of the following conditions are true:

    • the string contains invalid characters for the given radix
    • the string contains whitespace
    • the value the string represents is too large or too small to be a Long Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero

    Parameters

    • str: string

      The textual representation of the Long

    • Optionalradix: boolean

      The radix in which the text is written (2-36), defaults to 10

    Returns Long

    The corresponding Long value

  • Returns a Long representation of the given string, written using the specified radix. Will throw an error if the given parameters are not exactly representable as a Long. Throws an error if any of the following conditions are true:

    • the string contains invalid characters for the given radix
    • the string contains whitespace
    • the value the string represents is too large or too small to be a Long Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero

    Parameters

    • str: string

      The textual representation of the Long

    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    • Optionalradix: number

      The radix in which the text is written (2-36), defaults to 10

    Returns Long

    The corresponding Long value

  • Converts the specified value to a Long.

    Parameters

    • val: string | number | {
          high: number;
          low: number;
          unsigned?: boolean;
      }
    • Optionalunsigned: boolean

      Whether unsigned or not, defaults to signed

    Returns Long

  • Tests if the specified object is a Long.

    Parameters

    • value: unknown

    Returns value is Long