Options
All
• Public
• Public/Protected
• All

# Class Long

A class representing a 64-bit integer

remarks

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.

• Long

## Constructors

### constructor

• new Long(low?: string | number | bigint, high?: number | boolean, unsigned?: boolean): Long
• Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as signed integers. See the from* functions below for more convenient ways of constructing Longs.

Acceptable signatures are:

• Long(low, high, unsigned?)
• Long(bigint, unsigned?)
• Long(string, unsigned?)

#### Parameters

• ##### Optional low: string | number | bigint

The low (signed) 32 bits of the long

• ##### Optional high: number | boolean

The high (signed) 32 bits of the long

• ##### Optional unsigned: boolean

Whether unsigned or not, defaults to signed

## Properties

### __isLong__

__isLong__: true

An indicator used to reliably determine if an object is a Long or not.

### _bsontype

_bsontype: "Long"

### high

high: number

The high 32 bits as a signed value.

### low

low: number

The low 32 bits as a signed value.

### unsigned

unsigned: boolean

Whether unsigned or not.

### Static MAX_UNSIGNED_VALUE

MAX_UNSIGNED_VALUE: Long

Maximum unsigned value.

### Static MAX_VALUE

MAX_VALUE: Long

Maximum signed value.

### Static MIN_VALUE

MIN_VALUE: Long

Minimum signed value.

### Static NEG_ONE

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

## Methods

• Returns the sum of this and the specified Long.

### and

• Returns the sum of this and the specified Long.

Sum

### comp

• This is an alias of Long.compare

### compare

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

#### Returns 0 | 1 | -1

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

### div

• This is an alias of Long.divide

### divide

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

Quotient

### eq

• This is an alias of Long.equals

### equals

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

#### Parameters

• ##### other: string | number | Long | Timestamp

Other value

### eqz

• eqz(): boolean
• This is an alias of Long.isZero

### ge

• This is an alias of Long.greaterThanOrEqual

### getHighBits

• getHighBits(): number
• Gets the high 32 bits as a signed integer.

### getHighBitsUnsigned

• getHighBitsUnsigned(): number
• Gets the high 32 bits as an unsigned integer.

### getLowBits

• getLowBits(): number
• Gets the low 32 bits as a signed integer.

### getLowBitsUnsigned

• getLowBitsUnsigned(): number
• Gets the low 32 bits as an unsigned integer.

### getNumBitsAbs

• getNumBitsAbs(): number
• Gets the number of bits needed to represent the absolute value of this Long.

### greaterThan

• greaterThan(other: string | number | Long | Timestamp): boolean
• Tests if this Long's value is greater than the specified's.

### greaterThanOrEqual

• greaterThanOrEqual(other: string | number | Long | Timestamp): boolean
• Tests if this Long's value is greater than or equal the specified's.

### gt

• This is an alias of Long.greaterThan

### gte

• This is an alias of Long.greaterThanOrEqual

### inspect

• inspect(): string

### isEven

• isEven(): boolean
• Tests if this Long's value is even.

### isNegative

• isNegative(): boolean
• Tests if this Long's value is negative.

### isOdd

• isOdd(): boolean
• Tests if this Long's value is odd.

### isPositive

• isPositive(): boolean
• Tests if this Long's value is positive.

### isZero

• isZero(): boolean
• Tests if this Long's value equals zero.

### le

• This is an alias of Long.lessThanOrEqual

### lessThan

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

### lessThanOrEqual

• lessThanOrEqual(other: string | number | Long | Timestamp): boolean
• Tests if this Long's value is less than or equal the specified's.

### lt

• This is an alias of {@link Long#lessThan}.

### lte

• This is an alias of Long.lessThanOrEqual

### mod

• This is an alias of Long.modulo

### modulo

• Returns this Long modulo the specified.

### mul

• This is an alias of Long.multiply

### multiply

• Returns the product of this and the specified Long.

Multiplier

Product

### ne

• This is an alias of Long.notEquals

### neg

• This is an alias of Long.negate

### negate

• Returns the Negation of this Long's value.

### neq

• This is an alias of Long.notEquals

### not

• Returns the bitwise NOT of this Long.

### notEquals

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

### or

• or(other: string | number | Long): Long
• Returns the bitwise OR of this Long and the specified.

### rem

• This is an alias of Long.modulo

### shiftLeft

• shiftLeft(numBits: number | Long): Long
• Returns this Long with bits shifted to the left by the given amount.

#### Parameters

• ##### numBits: number | Long

Number of bits

Shifted Long

### shiftRight

• shiftRight(numBits: number | Long): Long
• Returns this Long with bits arithmetically shifted to the right by the given amount.

#### Parameters

• ##### numBits: number | Long

Number of bits

Shifted Long

### shiftRightUnsigned

• shiftRightUnsigned(numBits: number | Long): Long
• Returns this Long with bits logically shifted to the right by the given amount.

#### Parameters

• ##### numBits: number | Long

Number of bits

Shifted Long

### shl

• This is an alias of Long.shiftLeft

### shr

• This is an alias of Long.shiftRight

### shr_u

• This is an alias of Long.shiftRightUnsigned

### shru

• This is an alias of Long.shiftRightUnsigned

### sub

• This is an alias of Long.subtract

### subtract

• Returns the difference of this and the specified Long.

Subtrahend

Difference

### toBigInt

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

### toBytes

• toBytes(le?: boolean): number[]
• Converts this Long to its byte representation.

#### Parameters

• ##### Optional le: boolean

Whether little or big endian, defaults to big endian

#### Returns number[]

Byte representation

### toBytesBE

• toBytesBE(): number[]
• Converts this Long to its big endian byte representation.

#### Returns number[]

Big endian byte representation

### toBytesLE

• toBytesLE(): number[]
• Converts this Long to its little endian byte representation.

#### Returns number[]

Little endian byte representation

### toExtendedJSON

• toExtendedJSON(options?: Options): number | LongExtended

### toInt

• toInt(): number
• Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.

### toNumber

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

### toSigned

• Converts this Long to signed.

### toString

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

throws

RangeError If `radix` is out of range

### toUnsigned

• toUnsigned(): Long
• Converts this Long to unsigned.

### xor

• xor(other: string | number | Long): Long
• Returns the bitwise XOR of this Long and the given one.

### Static fromBigInt

• fromBigInt(value: bigint, unsigned?: boolean): 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

• ##### Optional unsigned: boolean

Whether unsigned or not, defaults to signed

#### Returns Long

The corresponding Long value

### Static fromBits

• fromBits(lowBits: number, highBits: number, unsigned?: boolean): Long
• 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

• ##### Optional unsigned: boolean

Whether unsigned or not, defaults to signed

#### Returns Long

The corresponding Long value

### Static fromBytes

• fromBytes(bytes: number[], unsigned?: boolean, le?: boolean): Long
• Creates a Long from its byte representation.

#### Parameters

• ##### bytes: number[]

Byte representation

• ##### Optional unsigned: boolean

Whether unsigned or not, defaults to signed

• ##### Optional le: boolean

Whether little or big endian, defaults to big endian

#### Returns Long

The corresponding Long value

### Static fromBytesBE

• fromBytesBE(bytes: number[], unsigned?: boolean): Long
• Creates a Long from its big endian byte representation.

#### Parameters

• ##### bytes: number[]

Big endian byte representation

• ##### Optional unsigned: boolean

Whether unsigned or not, defaults to signed

#### Returns Long

The corresponding Long value

### Static fromBytesLE

• fromBytesLE(bytes: number[], unsigned?: boolean): Long
• Creates a Long from its little endian byte representation.

#### Parameters

• ##### bytes: number[]

Little endian byte representation

• ##### Optional unsigned: boolean

Whether unsigned or not, defaults to signed

#### Returns Long

The corresponding Long value

### Static fromExtendedJSON

• fromExtendedJSON(doc: { \$numberLong: string }, options?: Options): number | Long

### Static fromInt

• fromInt(value: number, unsigned?: boolean): Long
• Returns a Long representing the given 32 bit integer value.

#### Parameters

• ##### value: number

The 32 bit integer in question

• ##### Optional unsigned: boolean

Whether unsigned or not, defaults to signed

#### Returns Long

The corresponding Long value

### Static fromNumber

• fromNumber(value: number, unsigned?: boolean): Long
• 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

• ##### Optional unsigned: boolean

Whether unsigned or not, defaults to signed

#### Returns Long

The corresponding Long value

### Static fromString

• fromString(str: string, unsigned?: boolean, radix?: number): Long
• Returns a Long representation of the given string, written using the specified radix.

#### Parameters

• ##### str: string

The textual representation of the Long

• ##### Optional unsigned: boolean

Whether unsigned or not, defaults to signed

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

#### Returns Long

The corresponding Long value

### Static fromValue

• fromValue(val: string | number | { high: number; low: number; unsigned?: boolean }, unsigned?: boolean): Long
• Converts the specified value to a Long.

#### Parameters

• ##### Optional unsigned: boolean

Whether unsigned or not, defaults to signed

### Static isLong

• isLong(value: any): value is Long
• Tests if the specified object is a Long.

#### Returns value is Long

Generated using TypeDoc