Long()

Constructor

Defines a Long class for representing a 64-bit two’s-complement integer value, which faithfully simulates the behavior of a Java “Long”. This implementation is derived from LongLib in GWT.

class Long()
Arguments:
  • low (number) – the low (signed) 32 bits of the Long.
  • high (number) – the high (signed) 32 bits of the 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.

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.

toInt

Return the int value.

toInt()
Returns:number the value, assuming it is a 32-bit integer.

toNumber

Return the Number value.

toNumber()
Returns:number the closest floating-point representation to this value.

toJSON

Return the JSON value.

toJSON()
Returns:string the JSON representation.

toString

Return the String value.

toString([opt_radix])
Arguments:
  • [opt_radix] (number) – the radix in which the text should be written.
Returns:

string the textual representation of this value.

getHighBits

Return the high 32-bits value.

getHighBits()
Returns:number the high 32-bits as a signed value.

getLowBits

Return the low 32-bits value.

getLowBits()
Returns:number the low 32-bits as a signed value.

getLowBitsUnsigned

Return the low unsigned 32-bits value.

getLowBitsUnsigned()
Returns:number the low 32-bits as an unsigned value.

getNumBitsAbs

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

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

isZero

Return whether this value is zero.

isZero()
Returns:boolean whether this value is zero.

isNegative

Return whether this value is negative.

isNegative()
Returns:boolean whether this value is negative.

isOdd

Return whether this value is odd.

isOdd()
Returns:boolean whether this value is odd.

equals

Return whether this Long equals the other

equals(other)
Arguments:
  • other (long) – Long to compare against.
Returns:

boolean whether this Long equals the other

notEquals

Return whether this Long does not equal the other.

notEquals(other)
Arguments:
  • other (long) – Long to compare against.
Returns:

boolean whether this Long does not equal the other.

lessThan

Return whether this Long is less than the other.

lessThan(other)
Arguments:
  • other (long) – Long to compare against.
Returns:

boolean whether this Long is less than the other.

lessThanOrEqual

Return whether this Long is less than or equal to the other.

lessThanOrEqual(other)
Arguments:
  • other (long) – Long to compare against.
Returns:

boolean whether this Long is less than or equal to the other.

greaterThan

Return whether this Long is greater than the other.

greaterThan(other)
Arguments:
  • other (long) – Long to compare against.
Returns:

boolean whether this Long is greater than the other.

greaterThanOrEqual

Return whether this Long is greater than or equal to the other.

greaterThanOrEqual(other)
Arguments:
  • other (long) – Long to compare against.
Returns:

boolean whether this Long is greater than or equal to the other.

compare

Compares this Long with the given one.

compare(other)
Arguments:
  • other (long) – Long to compare against.
Returns:

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

negate

The negation of this value.

negate()
Returns:long the negation of this value.

add

Returns the sum of this and the given Long.

add(other)
Arguments:
  • other (long) – Long to add to this one.
Returns:

long the sum of this and the given Long.

subtract

Returns the difference of this and the given Long.

subtract(other)
Arguments:
  • other (long) – Long to subtract from this.
Returns:

long the difference of this and the given Long.

multiply

Returns the product of this and the given Long.

multiply(other)
Arguments:
  • other (long) – Long to multiply with this.
Returns:

long the product of this and the other.

div

Returns this Long divided by the given one.

div(other)
Arguments:
  • other (long) – Long by which to divide.
Returns:

long this Long divided by the given one.

modulo

Returns this Long modulo the given one.

modulo(other)
Arguments:
  • other (long) – Long by which to mod.
Returns:

long this Long modulo the given one.

not

The bitwise-NOT of this value.

not()
Returns:long the bitwise-NOT of this value.

and

Returns the bitwise-AND of this Long and the given one.

and(other)
Arguments:
  • other (long) – the Long with which to AND.
Returns:

long the bitwise-AND of this and the other.

or

Returns the bitwise-OR of this Long and the given one.

or(other)
Arguments:
  • other (long) – the Long with which to OR.
Returns:

long the bitwise-OR of this and the other.

xor

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

xor(other)
Arguments:
  • other (long) – the Long with which to XOR.
Returns:

long the bitwise-XOR of this and the other.

shiftLeft

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

shiftLeft(numBits)
Arguments:
  • numBits (number) – the number of bits by which to shift.
Returns:

long this shifted to the left by the given amount.

shiftRight

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

shiftRight(numBits)
Arguments:
  • numBits (number) – the number of bits by which to shift.
Returns:

long this shifted to the right by the given amount.

shiftRightUnsigned

Returns this Long with bits shifted to the right by the given amount, with the new top bits matching the current sign bit.

shiftRightUnsigned(numBits)
Arguments:
  • numBits (number) – the number of bits by which to shift.
Returns:

long this shifted to the right by the given amount, with zeros placed into the new leading bits.

Long.fromInt

Returns a Long representing the given (32-bit) integer value.

Long.fromInt(value)
Arguments:
  • value (number) – the 32-bit integer in question.
Returns:

long the corresponding Long value.

Long.fromNumber

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

Long.fromNumber(value)
Arguments:
  • value (number) – the number in question.
Returns:

long the corresponding Long value.

Long.fromBits

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

Long.fromBits(lowBits, highBits)
Arguments:
  • lowBits (number) – the low 32-bits.
  • highBits (number) – the high 32-bits.
Returns:

long the corresponding Long value.

Long.fromString

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

Long.fromString(str, opt_radix)
Arguments:
  • str (string) – the textual representation of the Long.
  • opt_radix (number) – the radix in which the text is written.
Returns:

long the corresponding Long value.

Contents

Manual

MongoDB Wiki